Unexpected true/false result of contains method of Polygon

Dear All,

I have a really simple problem with polygon. For demo purpose, I check if a vertex of polygon is contained by this polygon. Normally this should be all true, but some of them is false.

Here is my sample program:

import java.awt.Polygon;

publicclass PolygonEx

{

publicstaticvoid main(String[] args){

Polygon p =new Polygon();

p.addPoint(196,156);

p.addPoint(208,164);

p.addPoint(216,182);

p.addPoint(198,200);

p.addPoint(175,182);

p.addPoint(185,158);

p.addPoint(196,156);

for (int p_cnt = 0; p_cnt < p.npoints; p_cnt++)

{

int tp_x = p.xpoints[p_cnt];

int tp_y = p.ypoints[p_cnt];

System.out.println("P:"+p_cnt+" x:"+p.xpoints[p_cnt]+" y:"+p.ypoints[p_cnt]+" IsContained:"+p.contains(tp_x, tp_y));

}

}

}

and, output:

P:0 x:196 y:156 IsContained:false

P:1 x:208 y:164 IsContained:false

P:2 x:216 y:182 IsContained:false

P:3 x:198 y:200 IsContained:false

P:4 x:175 y:182 IsContained:true

P:5 x:185 y:158 IsContained:true

P:6 x:196 y:156 IsContained:false

Is this a bug, or I miss sth ?

Regards,

[1827 byte] By [Terry_ba] at [2007-11-27 11:50:40]
# 1

> Is this a bug, or I miss sth ?

What do you think about the probability of the former versus the latter?

Also, please make the extra effort to write out words such as "something". The extra keystrokes won't cost much in the way of time, and the enhanced clarity will be appreciated by those communicating on a forum with international readership. Also, it will give the appearance that you take your question seriously, which will in turn make your question look more interesting to answer.

~

yawmarka at 2007-7-29 18:33:17 > top of Java-index,Java Essentials,Java Programming...
# 2

The contains(int, int) method says it returns true if the point is inside the polygon, so it's probably a idiosyncrasy of the algorithm they use. Why don't you look into the Polygon.contains() source and see how they do it?

hunter9000a at 2007-7-29 18:33:17 > top of Java-index,Java Essentials,Java Programming...