svishal03 said:
Actually, the user of the program is just drawing those lines indicated in the figure.He is not explicitely specifying any polygon as a 'bay'.
It is expected that once the user clicks in any point inside the polygon, the bay should be recognised and the lines between the bay drawn.
How to recognise the bay? That is, given a series of lines how to assign bays?
Once bays are assigned , we can then use your logic.
Can you hep again?
Based on what you have said here is what I recommend for you do to:
1) Let the user click on the screen to assign the point to a bay.
2) When the user has selected more than 2 points that are not collinear (ie all lie on the same line) do the following:
2b) Calculate the 2D form of the convex hull.
2c) Draw the hull on the screen
3) If user is happy with the shape (ie the bay) and user presses another button then save the hull information and go back to the user doing whatever
Now for user selecting bay:
If user presses button for selecting bay:
1) For each bay do a dot product test with the convex hull of each bay
2) If convex hull detects point inside bay then select bay (highlight) and do whatever
Now I know I said to use equations of lines and basically you will be doing that but first let me explain what the convex hull is.
A convex hull is an algorithm that creates a convex polytype in whatever dimension you are working in.
In 2 dimensions the algorithm is not too bad and given that you understand the algorithm you can implement in many dimensions.
If you know the difference between concave and convex polygons, you should have an idea of what it does: basically you give it a set of points and the hull outputs the biggest convex polygon with those points.
Since the user supplies points for the bay, every time they update the point list you can recalculate the hull on the fly and update the screen with the new bay.
Now the convex hull actually generates the line equations because the hull lists all the points in a counterclockwise order.
Basically you use this order and calculate a normal vector that points "outwards" and use this normal vector and a point to calculate whether the user is selecting a point in the bay.
The way it works is that you'll have a normal (a,b) and a distance c where (ax + by - c) < 0 if the point is on the negative half-space: if every "line-segment" gives you a negative result in calculating (ax + by - c), then the point is inside the hull and therefore inside the bay.
Its getting late so I'll help you later, but I would research the convex hull algorithm in two dimensions. If you have any questions I'll do my best to answer them.