基準となる座標値から時計回り(上,右,下,左)にぐるぐると座標値を求めてみる。
画像化には class AutoResizeVisualizer を利用。
ソースコード
import java.awt.geom.*;
import java.util.*;
// ぐるぐると外に広がっていく点の座標を求める
public class Grgr {
public static void main(String[] args) throws Exception {
Point2D[] p = getGuruGuruPoints();
AutoResizeVisualizer arv = new AutoResizeVisualizer();
for (int i = 0; i < p.length - 1; i++) {
System.out.println(p[i]);
arv.add(new Line2D.Double(p[i], p[i + 1]));
}
arv.visualize(300, 300, "grgr.png");
}
private static Point2D[] getGuruGuruPoints() {
// user' s params
final int num = 12;
final double beginx = 0.0;
final double beginy = 0.0;
final double movex = 10.0;
final double movey = 5.0;
// directions
final int NORTH = 0;
final int EAST = 1;
final int SOUTH = 2;
final int WEST = 3;
// vars
ArrayList<Point2D> list = new ArrayList<Point2D>();
double x = beginx;
double y = beginy;
list.add(new Point2D.Double(x, y));
// clockwise
for (int i = 0; i < num; i++) {
// forward_num = {1,1,2,2,3,3,4,4,5,5,...}
final int forward_num = (i / 2) + 1;
// forward
for (int j = 0; j < forward_num; j++) {
switch (i % 4) {
case NORTH :
y -= movey;
break;
case EAST :
x += movex;
break;
case SOUTH :
y += movey;
break;
case WEST :
x -= movex;
break;
}
list.add(new Point2D.Double(x, y));
}
}
return list.toArray(new Point2D[list.size()]);
}
}
出力結果
画像:
標準出力:
Point2D.Double[0.0, 0.0]
Point2D.Double[0.0, -5.0]
Point2D.Double[10.0, -5.0]
Point2D.Double[10.0, 0.0]
Point2D.Double[10.0, 5.0]
Point2D.Double[0.0, 5.0]
Point2D.Double[-10.0, 5.0]
Point2D.Double[-10.0, 0.0]
Point2D.Double[-10.0, -5.0]
Point2D.Double[-10.0, -10.0]
Point2D.Double[0.0, -10.0]
Point2D.Double[10.0, -10.0]
Point2D.Double[20.0, -10.0]
Point2D.Double[20.0, -5.0]
Point2D.Double[20.0, 0.0]
Point2D.Double[20.0, 5.0]
Point2D.Double[20.0, 10.0]
Point2D.Double[10.0, 10.0]
Point2D.Double[0.0, 10.0]
Point2D.Double[-10.0, 10.0]
Point2D.Double[-20.0, 10.0]
Point2D.Double[-20.0, 5.0]
Point2D.Double[-20.0, 0.0]
Point2D.Double[-20.0, -5.0]
Point2D.Double[-20.0, -10.0]
Point2D.Double[-20.0, -15.0]
Point2D.Double[-10.0, -15.0]
Point2D.Double[0.0, -15.0]
Point2D.Double[10.0, -15.0]
Point2D.Double[20.0, -15.0]
Point2D.Double[30.0, -15.0]
Point2D.Double[30.0, -10.0]
Point2D.Double[30.0, -5.0]
Point2D.Double[30.0, 0.0]
Point2D.Double[30.0, 5.0]
Point2D.Double[30.0, 10.0]
Point2D.Double[30.0, 15.0]
Point2D.Double[20.0, 15.0]
Point2D.Double[10.0, 15.0]
Point2D.Double[0.0, 15.0]
Point2D.Double[-10.0, 15.0]
Point2D.Double[-20.0, 15.0]
tags: zlashdot Java Java
Posted by NI-Lab. (@nilab)