Graphics2D - 数学图 - Ploygon - 如何获取所有绘图点

发布于 12-22 16:40 字数 561 浏览 4 评论 0原文

我只是尝试编写“行”代码来可视化简单的数学; 这是

Ploygon polygon=new Ploygon();
int x,y;


ploygon.addPoint(0,0);   
polygon.addPoint(width,height);

g.drawPolygon(polygon);

代码给出 y=x 的效果;

好的...这是非常简单的代码;但我感兴趣的是在语句期间每 N 个像素点为 {x0,y0}{0,0} 和 {x1,y1} {width,height} 这就是问题:(

多边形 xpoints 数组并不方便,因为它可能只包含调用 addPoint(x,y) 方法时添加的相同点,所以在我的例子中,只有两个通过 Polygon 连接的添加点,但是保留在这些点之间的所有其余点又如何呢?点{x0,y0}{0,0} 和 {x1,y1} {width,height} ?

例如,回到上一个片段,如何找出当 (height%N)=0 等等?

最好的方法吗?

I've just tried to write "line" code to visualize a simple math;
Here it is

Ploygon polygon=new Ploygon();
int x,y;


ploygon.addPoint(0,0);   
polygon.addPoint(width,height);

g.drawPolygon(polygon);

The code gives y=x effect;

OK... it is quite simple code; But the thing I am interested to get is points each N pixels during the statement period as {x0,y0}{0,0} and {x1,y1} {width,height} and that is the problem :(

The polygon xpoints array is not handy because it may contain just the same points which were added when addPoint(x,y) method was invoked; so in my case there just two added points which are connected by Polygon but what about all the rest points which stay between these points {x0,y0}{0,0} and {x1,y1} {width,height} ? How to get them?

For example. Coming back to the previous snippet how to find out what point x,y value is when (height%N)=0 etc?

Is there the most optimal way?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨巷深深2024-12-29 16:40:22

这里你必须意识到的是,你不再使用像素/坐标本身,而是使用向量。您将从包含坐标 (-500,-500)(500,500) 的多边形中获得几乎相同的图像,并将其绘制到 Graphics 对象,表示从左下角的 (0,0) 到右下角的 (100,100) 的(剪辑)区域。 (暂时忽略 Graphics 的实际坐标系具有反转的 y 轴)。

因此,您必须以更回归基本的数学方式而不是“读取像素”方式来解决这个问题。除非您只想确定给定点是否在形状中(Shape 接口提供了内置方法),否则您将需要计算斜率一条线并确定代表您的线的函数。例如,继续示例,您有两个点 (-500,-500) 和 (500,500),其斜率为 1000/1000 = 1。因此,您可以根据 x 坐标将该函数重写为 f (x) = -500 + (x + 500)。然后,如果您想知道点 (100,200) 是否在那条线上,您所需要做的就是计算 f(100) 并查看它是否不在。

回到您的示例,查找与谓词匹配的点(height%N =0),我们将寻找 f(x) == 0 mod N ,因此您需要“全部”需要做的是求解 x 的方程。

What you have to realise here is that you are no longer working with pixels/coordinates per se, but you are working with vectors. You'd get much the same image from a polygon contained the coordinates (-500,-500) and (500,500) which is drawn onto a Graphics object which represents the (clipped) area from (0,0) in the bottom left to (100,100) in the bottom right. (ignoring for now that the actual coordinate system of Graphics has an inverted y-axis).

Therefore you have to solve this in a more back-to-basic's Math way rather than a “read the pixels” way. Unless you just want to determine if a given point is in the shape (for which the Shape interface offers a built-in method), you would be looking at calculating the slope of a line and determining functions which represent your line. For instance continuing from the example you have two points (-500,-500) and (500,500) which gives a slope of 1000/1000 = 1. So you could rewrite that function in terms of your x-coordinates as f(x) = -500 + (x + 500). Then if you want to know if the point (100,200) is on that line all you need to do is calculate f(100) and see that it isn't.

Getting back to your example, finding points which match a predicate (height%N =0), we'd be looking for f(x) == 0 mod N and so 'all' you'd need to do is solve the equation for x.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文