十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
用极坐标方程,一个点一个点的画。给你段我以前写的程序,虽然不是螺旋曲线,但也差不多。import java.awt.*;
10年专注成都网站制作,企业网站建设,个人网站制作服务,为大家分享网站制作知识、方案,网站设计流程、步骤,成功服务上千家企业。为您提供网站建设,网站制作,网页设计及定制高端网站建设服务,专注于企业网站建设,高端网页制作,对门窗定制等多个领域,拥有多年设计经验。
import javax.swing.*;public class Spirograph extends JApplet {
public static void main(String s[]) {
JFrame frame = new JFrame();
frame.setTitle("Spirograph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new Spirograph();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
}
public void init() {
JPanel panel = new SpiroPanel();
getContentPane().add(panel);
}
}class SpiroPanel extends JPanel{
int nPoints = 1000;
double r1 = 60;
double r2 = 50;
double p = 70;
public SpiroPanel() {
setPreferredSize(new Dimension(400, 400));
setBackground(Color.white);
} public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.translate(200,200);
int x1=(int)(r1+r2-p);
int y1=0;
int x2;
int y2;
for (int i=0; inPoints; i++) {
double t = i*Math.PI/90;
x2 = (int)((r1+r2)*Math.cos(t)-p*Math.cos((r1+r2)*t/r2));
y2 = (int)((r1+r2)*Math.sin(t)-p*Math.sin((r1+r2)*t/r2));
g2.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
}
}
import java.io.*;public class RingDemo {
public static void main(String[] args) {
String strIn = "";
System.out.print("请输入矩阵的行列数:");
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader buff = new BufferedReader(input);
try {
strIn = buff.readLine();
} catch (IOException e) {
System.out.println(e.toString());
}
int int1 = Integer.parseInt(strIn);
int n = int1;
System.out.println("这是行列数为" + n + "的螺线型数组:");
int intA = 1; // 初始化
int[][] array = new int[n][n];
int intB;
if (n % 2 != 0) {
intB = n / 2 + 1; // 奇数时i循环次数
} else
intB = n / 2; // 偶数时i循环次数
for (int i = 0; i intB; i++) { // 从外到里循环
// 从左到右横的开始
for (int j = i; j n - i; j++) {
array[i][j] = intA;
intA++;
}
// 从上到下纵
for (int k = i + 1; k n - i; k++) {
array[k][n - i - 1] = intA;
intA++;
}
// 从右到左横
for (int l = n - i - 2; l = i; l--) {
array[n - i - 1][l] = intA;
intA++;
}
// 从下到上纵
for (int m = n - i - 2; m i; m--) {
array[m][i] = intA;
intA++;
}
}
// 输出数组
for (int i = 0; i n; i++) {
for (int j = 0; j n; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
} }
}
Java程序:
public class Main {
public static void main(String[] args) {
int LEN = 8;
int[][] arr = new int[LEN][LEN];
int i = 0, j = 0;
int n = 1;
arr[i][j] = n;
while(n LEN * LEN) {
while(j+1LEN arr[i][j+1]==0){
j++;
arr[i][j] = ++n;
}
while(i+1LEN arr[i+1][j]==0){
i++;
arr[i][j] = ++n;
}
while(j-1=0 arr[i][j-1]==0){
j--;
arr[i][j] = ++n;
}
while(i-1=0 arr[i-1][j]==0){
i--;
arr[i][j] = ++n;
}
}
for(i=0; iLEN; i++) {
for(j=0; jLEN; j++){
System.out.printf("%4d", arr[i][j]);
}
System.out.println();
}
}
}
运行结果:
1 2 3 4 5 6 7 8
28 29 30 31 32 33 34 9
27 48 49 50 51 52 35 10
26 47 60 61 62 53 36 11
25 46 59 64 63 54 37 12
24 45 58 57 56 55 38 13
23 44 43 42 41 40 39 14
22 21 20 19 18 17 16 15
按照你的要求用Java编写的螺旋矩阵程序如下:
public class N {
public static void main(String[] args) {
final int N=4;
int a[][]=new int[N][N];
int num=1;
int i=0,j=0,m=0;
if(N%2==0)
m=N/2;
else
m=N/2+1;
for(i=0;i=m-1;i++){
for(j=i;j=N-i-1;j++){
a[i][j]=num;
num++;
}
for(j=i+1;j=N-i-1;j++) {
a[j][N-i-1]=num;
num++;
}
for(j=N-i-2;j=i;j--){
a[N-i-1][j]=num;
num++;
}
for(j=N-i-2;j=i+1;j--){
a[j][i]=num;
num++;
}
}
for(i=0;iN;i++){
for(j=0;jN;j++){
System.out.print(String.format("%3d",a[i][j]));
}
System.out.println();
}
}
}
运行结果:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
刚在另一个回答完,都是匿名,也不知道是不是一个人。
n为阶数,必须是奇数,这里是5.
public class Q {
static int[][] nums;
static int[][] go={{-1,0},{0,1},{1,0},{0,-1}};
static int to=0;
static int[] next;
static int n=5;
public static void main(String[] args) {
if(n%2==0){
return;
}
nums=new int[n][n];
for(int i=0;in;i++){
for(int j=0;jn;j++){
nums[i][j]=0;
}
}
int m=n/2;
int number=1;
next=new int[]{m,m};
while(next!=null){
int x=next[0];
int y=next[1];
nums[x][y]=number;
number++;
next();
}
for(int i=0;in;i++){
for(int j=0;jn;j++){
System.out.print(nums[i][j]+"\t");
}
System.out.println();
}
}
public static void next(){
int tto;
if(to==3){
tto=0;
}else{
tto=to+1;
}
int x=next[0]+go[tto][0];
int y=next[1]+go[tto][1];
if(nums[x][y]==0){
next[0]=x;
next[1]=y;
to=tto;
}else{
x=next[0]+go[to][0];
y=next[1]+go[to][1];
if(x=n||y=n){
next=null;
}else{
next[0]=x;
next[1]=y;
}
}
}
}
有问题请追问,没问题请采纳。
public class Cat {
/**
* @param args
*/
public static void main(String[] args) {
int number = 5;
int[][] arys = printCircle(number);
for (int i = 0; i number; i++) {
for (int j = 0; j number; j++) {
System.out.print(arys[i][j] + "\t");
}
System.out.println();
}
}
// 1 2 3 4 5
// 16 17 18 19 6
// 15 24 25 20 7
// 14 23 22 21 8
// 13 12 11 10 9
private static int[][] printCircle(int number) {
int[][] ary = new int[number][number];
int rightLen;
int downLen;
int leftLen;
int upLen;
int rightRow = 0;
int rightCol = 0;
int leftRow = number - 1;
int leftCol = number - 1;
int downRow = 1;
int downCol = number - 1;
int upRow = number - 2;
int upCol = 0;
int digit = 1;
while(number 0){
rightLen = number;
downLen = number - 1;
leftLen = number - 1;
upLen = number - 2;
//Step 1: print to right
for(int i = 0, temp = rightCol; i rightLen; i++){
ary[rightRow][temp++] = digit++;
}
//Step 2: print to down
for(int j = 0, temp = downRow; j downLen; j++){
ary[temp++][downCol] = digit++;
}
//move left
for(int m = 0, temp = leftCol - 1; m leftLen; m++){
ary[leftRow][temp--] = digit++;
}
//move up
for(int n = 0, temp = upRow; n upLen; n++){
ary[temp--][upCol] = digit++;
}
number = number - 2;
//increas/decrease the row and column
rightRow++;
rightCol++;
downRow++;
downCol--;
leftRow--;
leftCol--;
upRow--;
upCol++;
}
return ary;
}
}
-----------------for 8 --------------
1 2 3 4 5 6 7 8
28 29 30 31 32 33 34 9
27 48 49 50 51 52 35 10
26 47 60 61 62 53 36 11
25 46 59 64 63 54 37 12
24 45 58 57 56 55 38 13
23 44 43 42 41 40 39 14
22 21 20 19 18 17 16 15