|
|
Re: Starange problem with selection [message #534862 is a reply to message #534807] |
Thu, 20 May 2010 10:09   |
Eclipse User |
|
|
|
Ok its a bug in draw2d pls conferm...
There is a class in draw2d called Geometry which is being used in polyline.containsPoint(int x, int y)
This has a method called segmentContainsPoint which uses Dot product of two vectors formula to calculate the distance between the point and the line segment...
/**
* @return true if the least distance between point (px,py) and segment (x1,y1) - (x2,y2) is
* less then specified tolerance
*/
private static boolean segmentContainsPoint(int x1, int y1, int x2, int y2, int px, int py,
int tolerance)
{
/*
* Point should be located inside Rectangle(x1 -+ tolerance, y1 -+ tolerance, x2 +-
* tolerance, y2 +- tolerance)
*/
Rectangle lineBounds = Rectangle.SINGLETON;
lineBounds.setSize(0, 0);
lineBounds.setLocation(x1, y1);
lineBounds.union(x2, y2);
lineBounds.expand(tolerance, tolerance);
if (!lineBounds.contains(px, py))
{
return false;
}
/*
* If this is horizontal, vertical line or dot then the distance between specified point
* and segment is not more then tolerance (due to the lineBounds check above)
*/
if (x1 == x2 || y1 == y2)
{
return true;
}
/*
* Calculating square distance from specified point to this segment using formula for Dot
* product of two vectors.
*/
int v1x = x2 - x1;
int v1y = y2 - y1;
int v2x = px - x1;
int v2y = py - y1;
int numerator = v2x * v1y - v1x * v2y;
int denominator = v1x * v1x + v1y * v1y;
int squareDistance = (int) ((long) numerator * numerator / denominator);
return squareDistance <= tolerance * tolerance;
}
Here at the end ,the dot product formula is wrong the denominater should be modded,since it can be negative
and the distance can come out to be negative, which will be always less tolerance^2...
In certain scanarious the segmentcontainspoint returns true hence the selection.
|
|
|
|
Powered by
FUDForum. Page generated in 0.05040 seconds