public Shape getRegularPolygon(Vertex v, int num_sides)
{
if (num_sides < 3)
throw new IllegalArgumentException("Number of sides must be >= 3");
Rectangle2D frame = getRectangle(v);
float width = (float)frame.getWidth();
float height = (float)frame.getHeight();
// generate coordinates
double angle = 0;
thePolygon.reset();
thePolygon.moveTo(0,0);
thePolygon.lineTo(width, 0);
double theta = (2 * Math.PI) / num_sides;
for (int i = 2; i < num_sides; i++)
{
angle -= theta;
float delta_x = (float) (width * Math.cos(angle));
float delta_y = (float) (width * Math.sin(angle));
Point2D prev = thePolygon.getCurrentPoint();
thePolygon.lineTo((float)prev.getX() + delta_x, (float)prev.getY() + delta_y);
}
thePolygon.closePath();
// scale polygon to be right size, translate to center at (0,0)
Rectangle2D r = thePolygon.getBounds2D();
double scale_x = width / r.getWidth();
double scale_y = height / r.getHeight();
float translationX = (float) (r.getMinX() + r.getWidth()/2);
float translationY = (float) (r.getMinY() + r.getHeight()/2); AffineTransform at = AffineTransform.getScaleInstance(scale_x, scale_y);
at.translate(-translationX, -translationY);
Shape shape = at.createTransformedShape(thePolygon);
return shape;
}这是我在阅读jung代码时碰到的,看了它的坐标计算方法,thePolygon.lineTo((float)prev.getX() + delta_x, (float)prev.getY() + delta_y),为什么要这么计算,百思不得其解,请大侠不吝赐教
{
if (num_sides < 3)
throw new IllegalArgumentException("Number of sides must be >= 3");
Rectangle2D frame = getRectangle(v);
float width = (float)frame.getWidth();
float height = (float)frame.getHeight();
// generate coordinates
double angle = 0;
thePolygon.reset();
thePolygon.moveTo(0,0);
thePolygon.lineTo(width, 0);
double theta = (2 * Math.PI) / num_sides;
for (int i = 2; i < num_sides; i++)
{
angle -= theta;
float delta_x = (float) (width * Math.cos(angle));
float delta_y = (float) (width * Math.sin(angle));
Point2D prev = thePolygon.getCurrentPoint();
thePolygon.lineTo((float)prev.getX() + delta_x, (float)prev.getY() + delta_y);
}
thePolygon.closePath();
// scale polygon to be right size, translate to center at (0,0)
Rectangle2D r = thePolygon.getBounds2D();
double scale_x = width / r.getWidth();
double scale_y = height / r.getHeight();
float translationX = (float) (r.getMinX() + r.getWidth()/2);
float translationY = (float) (r.getMinY() + r.getHeight()/2); AffineTransform at = AffineTransform.getScaleInstance(scale_x, scale_y);
at.translate(-translationX, -translationY);
Shape shape = at.createTransformedShape(thePolygon);
return shape;
}这是我在阅读jung代码时碰到的,看了它的坐标计算方法,thePolygon.lineTo((float)prev.getX() + delta_x, (float)prev.getY() + delta_y),为什么要这么计算,百思不得其解,请大侠不吝赐教
解决方案 »
- 为什么Canvas.getGraphics()获取不到呢
- java如何将0x00, 0x26, 0xA6, 0x54, 0x4F, 0xF2字节数组转换为,00:26:A6:54:4F:F2格式的字符串
- 关于类型转换的一个问T,,,,,,,
- 我想出一本JAVA面试题解惑系列的书籍
- 一个Jave程序在英文XP下显示中文为方框,烦请大虾解决!
- 在JAVA中怎么实现 让表格的每列的宽度能根据里面的内容而自动变化?
- jdbc连接access问题。为什么不能插入
- 关于MICROSOFT OFFICE 的问题
- 单例模式和静态对象的销毁
- 高手帮忙:APPLET中SOCKET实现连续的数据传输
- 一个简单的程序为什么运行不通过?
- at java.lang.ClassLoader.defineClass1(Native Method)
double theta = (2 * Math.PI) / num_sides;顺时针画线
angle -= theta;利用圆的参数方程计算各点偏移坐标
float delta_x = (float) (width * Math.cos(angle));
float delta_y = (float) (width * Math.sin(angle));把之前的点和这次的点用线连接起来
thePolygon.lineTo((float)prev.getX() + delta_x, (float)prev.getY() + delta_y);
这个2个点的横纵坐标分别差了(delta_x, delta_y),对这一点很难理解,很显然这不是通过旋转角度算出来的,他用的方法是根据前一点的坐标
来计算后一点的坐标
直接用 thePolygon.lineTo(delta_x, delta_y); 就
可以了啊,为什么还要加上前面点的坐标呢?
thePolygon.lineTo((float)prev.getX() + delta_x, (float)prev.getY() + delta_y);