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

user592704
  • 3,674
  • 11
  • 70
  • 107

1 Answers1

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

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.

user268396
  • 11,576
  • 2
  • 31
  • 26
  • "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." It is all fine but what if the f(x)!=N ? I mean it is not integer so there is no way to get right x,y coordinate? What goes if the plot statement is like "y=x*tan(a/b)+coeff" for example? Is there a way to detect this kind of function points to keep them let's say in an array? – user592704 Dec 25 '11 at 03:42
  • If I understand you correctly, that would be solved by rounding your answer to the nearest integer? – user268396 Dec 25 '11 at 12:05
  • Emm... The Math.round() you mean? OK I'll try it and report my results – user592704 Dec 26 '11 at 19:31