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

时间:2023-01-05 19:01:50

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;

代码给出y = x效果;

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 :(

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

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?

多边形xpoints数组不方便,因为它可能只包含调用addPoint(x,y)方法时添加的相同点;所以在我的情况下只有两个由Polygon连接的附加点,但是在这些点{x0,y0} {0,0}和{x1,y1} {width,height}之间的所有其余点呢?怎么弄他们?

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

例如。回到前面的代码片段,如何找出x,y值是什么时候(高度%N)= 0等?

Is there the most optimal way?

有最佳方​​式吗?

Thanks

1 个解决方案

#1


0  

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).

你必须要意识到的是,你不再使用像素/坐标本身,而是使用向量。您将从包含坐标(-500,-500)和(500,500)的多边形获得相同的图像,该坐标被绘制到Graphics对象上,该对象表示从左下角的(0,0)到(剪切的)区域到右下角(100,100)。 (暂时忽略Graphics的实际坐标系有一个倒y轴)。

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.

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

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.

回到你的例子,找到与谓词匹配的点(高度%N = 0),我们要寻找f(x)== 0 mod N,所以你需要做的“全部”就是求解方程式对于x。

#1


0  

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).

你必须要意识到的是,你不再使用像素/坐标本身,而是使用向量。您将从包含坐标(-500,-500)和(500,500)的多边形获得相同的图像,该坐标被绘制到Graphics对象上,该对象表示从左下角的(0,0)到(剪切的)区域到右下角(100,100)。 (暂时忽略Graphics的实际坐标系有一个倒y轴)。

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.

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

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.

回到你的例子,找到与谓词匹配的点(高度%N = 0),我们要寻找f(x)== 0 mod N,所以你需要做的“全部”就是求解方程式对于x。