this is my first question in Stackoverflow. I'm creating an IDE for adventure games in JAVA, and i need to set the walkable area. This shape is a polygon which i can paint with his vertex already, and i can to add new points. The problem is that i can't to detect if mouse position is Only over the polygon's border. This is for create new vertex without deform the shape.
Does exist any way to select the border/stroke of a polygon and register this event out of the PaintComponent ?
Thanks for any help
you can iterate over the polygon's point and determine if you hit a line.
Polygon p; //your polygon
int x_mouse;//your mouse click pos
int y_mouse;
for (int i = 0; i < p.npoints; i ++){
int x_from = 0;
int y_from = 0;
int x_to = 0;
int y_to = 0;
if (i == 0){ //i-1 == -1 -> p.npoints-1
x_from = p.xpoints[p.npoints-1];
y_from = p.ypoints[p.npoints-1];
}else{
x_from = p.xpoints[i-1];
y_from = p.ypoints[i-1];
}
x_to = p.xpoints[i];
y_to = p.ypoints[i];
Line2D line = new Line2D.Double(x_from, y_from, x_to, y_to);
if (line.ptLineDist(new Point(x_mouse, y_mouse)) <= 0.01){
//you hit
}
}
have a look at Java - Point on line to see why 'ptLineDist(p) < 0.01' should be preferred.