Java 1.5 使用线条和角度绘制形状的问题
嘿,我正在尝试编写一种方法,该方法采用起始笛卡尔坐标(x,y)、角度(以度为单位)、长度和边数,并将形状绘制到小程序中。到目前为止,这就是我所拥有的,但是我不知道我做错了什么。我计划使用直线变换来实现实际的角度变化,但这还没有写入,但以一定角度绘制直线的逻辑应该可行,但据我所知还没有。我能否请一双新的眼睛来看看这个并告诉我是否遗漏了什么。
public void paint(Graphics g)
{
g.setColor(Color.BLACK);
Point startPt = new Point(0,0);
//Function in question
drawRegularPolygon(g, startPt, 5,60,50);
}
public static void drawRegularPolygon(Graphics g, Point2D startPoint, int numOfSides, int angle, int length)
{
Point2D current = startPoint;
for(int i=0; i<numOfSides; i++)
{
drawAngularLine(g, current, angle, length);
current = getEndPoint(current ,length,angle);
}
}
public static void drawAngularLine(Graphics g, Point2D startPoint, int angle, int length)
{
g.setColor(Color.BLACK);
Point2D endPoint = getEndPoint(startPoint, length, angle);
((Graphics2D) g).draw(new Line2D.Double(startPoint, endPoint));
}
private static Point2D getEndPoint(Point2D p, int length, int angle)
{
//Starting point you know (x1, x2),
//end point is (x1 + l * cos(ang), y1 + l * sin(ang))
//where l is the length and ang is the angle.
Point2D retVal = p;
double x = Math.cos(Math.toRadians(angle)*length+p.getX());
double y = Math.sin(Math.toRadians(angle)*length+p.getY());
retVal.setLocation(x,y);
return retVal;
}
Hey, I'm trying to write a method that takes a starting Cartesian coordinate(x,y) an angle (in degrees), a length and a number of sides and draws a shape to an applet. So far this is what I have but, I cant figure out what I'm doing wrong. I plan on using line transformations for the actual angle change and that's not written in yet but the logic for drawing a line at an angle should work but isn't as far as I can tell. Could I get a couple of new eyes to look at this and tell me if I'm missing something.
public void paint(Graphics g)
{
g.setColor(Color.BLACK);
Point startPt = new Point(0,0);
//Function in question
drawRegularPolygon(g, startPt, 5,60,50);
}
public static void drawRegularPolygon(Graphics g, Point2D startPoint, int numOfSides, int angle, int length)
{
Point2D current = startPoint;
for(int i=0; i<numOfSides; i++)
{
drawAngularLine(g, current, angle, length);
current = getEndPoint(current ,length,angle);
}
}
public static void drawAngularLine(Graphics g, Point2D startPoint, int angle, int length)
{
g.setColor(Color.BLACK);
Point2D endPoint = getEndPoint(startPoint, length, angle);
((Graphics2D) g).draw(new Line2D.Double(startPoint, endPoint));
}
private static Point2D getEndPoint(Point2D p, int length, int angle)
{
//Starting point you know (x1, x2),
//end point is (x1 + l * cos(ang), y1 + l * sin(ang))
//where l is the length and ang is the angle.
Point2D retVal = p;
double x = Math.cos(Math.toRadians(angle)*length+p.getX());
double y = Math.sin(Math.toRadians(angle)*length+p.getY());
retVal.setLocation(x,y);
return retVal;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几件事。首先是要小心你所取的正弦/余弦值。它不是 cos(角度*长度) 而是长度*cos(角度)。
第二点是考虑坐标系。假设初始点为 (0,0),然后转换为屏幕坐标,进行数学计算可能会有所帮助。这有助于避免 y 轴看起来颠倒的混乱(值从上到下增加)。
因此,假设我们只想要一个距离标准右手系统中的原点的长度和角度的点,我们会得到:
但由于负 y 向上,我们实际上想要
在心里检查一下你正在做的图片这个数学计算的原点 (0,0) 位于左上角,角度为 45°。如果 y2 为正,我们最终会看到一个看起来像 -45° 的角度。
现在将原点转换为我们的起点 (x_i, y_i),以获得最终值:
或者,如果在标准右手坐标系中工作更有意义,您可能可以像这样完成所有数学运算(0,0) 位于中心,然后应用平移和 y 轴镜像变换,但是一旦您习惯了翻转 y 值,这个屏幕坐标系就不太难操作。
A couple things. The first is to be careful about what you're taking sin/cosine of. It's not cos(angle*length) but rather length*cos(angle).
The second point is to think about coordinate systems. It might help to do the math assuming the initial point is (0,0), and then translate to the screen coordinates. This helps avoid the confusion of the y-axis seeming to be upside-down (values increase from top to bottom).
So assuming we just want a point that's length,angle away from the origin in a standard right-handed system, we'd get:
But since negative-y is up, we actually want
To mentally check this, picture that you're doing this math at the origin (0,0) which is in the upper left, and have an angle of 45°. If y2 were positive, we'd end up seeing an angle that looks to us like -45°.
Now translate the origin to our starting point (x_i, y_i), to get our final values:
Alternatively, if it makes more sense to work in a standard right-handed coordinate system, you probably could get away with doing all the math as if (0,0) were in the center, and then applying a translation and a y-axis mirror transformation, but this screen coordinate system isn't too difficult to work within once you get used to flipping the y values around.
您正在绘制一条具有相同起点和终点的线 - 因此不会绘制任何内容。
Java对象是通过引用传递的,所以:
也在改变起点
p
。所以它画了一条长度为 1 的线(它在屏幕上显示一个点吗?)。尝试使用:
You are drawing a line with the same start point and end point - so nothing is drawn.
Java objects are passed by reference, so:
is also changing the starting point
p
. So it draws a line of length 1 (does it show a dot on the screen?).Try using: