2

I need an algorithm that, given an array of points representing a polygon (which is usually a rectangle-ish shape, but could be any nonregular polygon), and given a point click location within that polygon, fit a circle touching the three sides of the polygon that are closest to the clicked location, but maximizing the overall coverage of the circle without leaving the boundary

It's easy to do touching two sides (find centroid, then find shortest distance to edge, etc) - but touching three sides is the challenge. See the attached hand drawing.

enter image description here

I need answer soon..Thanks!

torrential coding
  • 1,755
  • 2
  • 24
  • 34
Wajahat
  • 1
  • 6
  • What have you tried to achieve this? A little code goes a looooooong way... – Brian Apr 19 '13 at 22:49
  • not much I have just tried to take the x,y coordinates of the point where mouse click was recorded and checked the corresponding nearest points of the neighbouring lines...but I am stuck here – Wajahat Apr 19 '13 at 22:51
  • See the: [DrawEllipse()](http://stackoverflow.com/questions/1835062/draw-circle-in-visual-c-sharp) Method. – Brian Apr 19 '13 at 22:53
  • i know about using draw ellipse for a circle but the problem is that i want the circle to touch at least three neighbouring vertices of the polygon as you can see in figure and i am stuck here...what logic can i use here? – Wajahat Apr 19 '13 at 22:55
  • 1
    why it's hard with 3 sides? when you click on some point first check the distance.. if you have 3 equals distances this is your circle radius – Elior Apr 19 '13 at 22:56
  • 1
    Have you considered breaking up the polygon into line segments and then simply finding the nearest three to the point. Then calculate the center and radius. – Nuclearman Apr 19 '13 at 22:57
  • Couldn't you draw the circle then draw the polygon _around_ the circle? – Brian Apr 19 '13 at 22:58
  • Thanks! I also want to know that how to access the nearest vertices of the polygon...I mean how to know that the three points i have selected are the closest and are three different vertices? – Wajahat Apr 19 '13 at 22:59
  • when you click on some point.. you have (x,y) of the point so this is the center of the circle.. now if you want the right segment point, the 'Y' is the same as the center of the circle and 'X' is the position of the segment..you can use this idea on the other segments – Elior Apr 19 '13 at 23:02
  • 1
    You have to go through every one of the line segments, and calculate the distance for each, then go through the results and find the closest two. Data structures can be useful if there are multiple clicks and/or multiple polygons. @Elior: The OP says the place the user clicked may not actually be the center of the circle. – Nuclearman Apr 19 '13 at 23:05
  • Sorry, but the point at which user may clicks may or may not be the centre of the circle as you can see in the figure above – Wajahat Apr 19 '13 at 23:05
  • @MC problem here is that we have to go through three lines not two...and also that how to interpret within logic that the 2 of the 3 points not on the same line segment of the polygon? – Wajahat Apr 19 '13 at 23:09
  • 1
    That was a typo, it was supposed to be three line segments. The points of those line segments don't matter. A 5 vertex polygon with points A,B,C,D,E, would have the line segments A-B, B-C, C-D, D-E and E-A. If the point is located at P, then you calculate the distance from P to each of those 5 line segments, then take the shortest three distances. There are some issues though, such as the situation where one of the three line segments are unreachable. A similar situation might arise with the bottom example, where if you fix it against the top and right, a circle may miss the desired bottom. – Nuclearman Apr 19 '13 at 23:17
  • 1
    Fixing the issues I pointed out, likely requires picking two sides and expanding it, until it touches a third side, or is no longer on of the two line segments. Which may have been why I said 2 sides. – Nuclearman Apr 19 '13 at 23:25
  • @MC Thanks! Another thing how to check it in logic that our ellipse or circle did not exceed the boundary of polygon. As you mentioned in bottom figure there may be two bottoms close to our circle but how to keep that check? – Wajahat Apr 19 '13 at 23:25
  • Hard to say, my previous comment goes into it a little bit, although it's less of a problem if you use ellipses instead of circles. There is still the possibility of the closest three edges looking like this: _|\, but that will just need to watch out for. – Nuclearman Apr 19 '13 at 23:32

1 Answers1

3

Problem is related to a medial axis. Points that are centers of circles touching 3 or more sides are branching points of a medial axis. It is enough to make medial axis, find branching points and for a given click position find the closest one.

Ante
  • 5,350
  • 6
  • 23
  • 46