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
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 aGraphicsobject 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 ofGraphicshas 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
Shapeinterface 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 asf(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 calculatef(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 Nand so ‘all’ you’d need to do is solve the equation for x.