1

User types value of x and y which will be a coordinate. There is some kind of a shape there and I need to tell if that coordinate will be outside, inside or on the border of that shape. I know how to do that with rectangle shape, but not with triangle or circle. Currently I need to do this with this shape: http://i.imgur.com/5teZk85.png

How do I do this for circle or triangle shape?

For rectangle I used these code lines:

IF (x>=-5) AND (x<=2) AND (y>=-1) AND (y<=3) then
   IF (x=-5) OR (x=2) OR (y=-1) OR (y=3) then
      writeln('Dot is located on line')
   else writeln('Dot is located inside')
   else writeln('Dot is located outside');
rits
  • 1,474
  • 7
  • 29
  • 49
  • 1
    To get answer from SO, you would need to provide some more details on how do you model the problem in your program using your chosen programming language. – George Polevoy Sep 16 '14 at 15:00
  • It's a wide topic. check this pdf: http://www.cs.princeton.edu/courses/archive/fall05/cos226/lectures/geosearch.pdf – Spindizzy Sep 16 '14 at 18:24

2 Answers2

3

For a circle, you can do the following test:

If x^2 + y^2 < r^2, then it is inside the circle.

For triangles, you should probably look at this question: How to determine if a point is in a 2D triangle?

Community
  • 1
  • 1
Reed Oei
  • 1,427
  • 2
  • 13
  • 19
  • And how can i tell if it's going to be on the border of circle and not inside/outside? – rits Sep 16 '14 at 16:42
  • 1
    If it is on the border of the circle, `x^2 + y^2 = r^2`, and if it is outside, then `x^2 + y^2 > r^2`. – Reed Oei Sep 16 '14 at 18:33
0

A while ago I enrolled for an online course about algorithms, held by Robert Sedgewick. One topic was "Geometric Search", it describes you problem. Therefore I don't want to post a code snippet here, but suggest to read one of his books. See my comment for a pdf that might help you to get on the right track.

Spindizzy
  • 7,244
  • 1
  • 19
  • 33
  • 2
    A link to an external site that provides the solution is not considered an answer here. You should either provide an answer here post the link as a comment to the original question instead. – Ken White Sep 16 '14 at 16:55