5 Replies - 366 Views - Last Post: 13 May 2014 - 01:04 PM Rate Topic: -----

#1 Hramyzn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-April 14

How to tell if a point is inside a triangle or outside?

Posted 13 May 2014 - 06:51 AM

So, I have created a class called "point" and i have 4 "point" objects. They only have 2 variables, x and y (their position). The first 3 points form a triangle and now I need to tell if the forth one is inside or outside. I have found some solutions but they involve heavy math (they are based on the sum of the angles or something like that). I want to know if there is any way to solve this only by using the distance between points. I have created a function which takes 2 "point" objects and returns a float value which is their distance.

Here is some code:

#include <iostream>
#include <cstdlib>
#include <math.h>

using namespace std;

class point //the "point" class
{
public:
  float x,y;
};

float dis(point a, point B)/>/> //function for calculating the distance between 2 points
{
  return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2))
}

main()
{
  point a,b,c,d;
  cin >> a.x >> a.y;
  cin >> b.x >> b.y;
  cin >> c.x >> c.y;
  cin >> d.x >> d.y;
  /*code to determine if d is inside the triangle formed by a,b and c 
  (which i have no idea how to do without copying th heavy math found on forums)*/
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to tell if a point is inside a triangle or outside?

#2 Hramyzn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-April 14

Re: How to tell if a point is inside a triangle or outside?

Posted 13 May 2014 - 06:56 AM

Don't mind the errors such as the "/>" after "dis" function, or missing ";". This is actually only a part of a bigger program so I just retyped it to make sense without the rest of the code and didn't copy/paste.
Was This Post Helpful? 0
  • +
  • -

#3 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 233
  • View blog
  • Posts: 837
  • Joined: 18-April 09

Re: How to tell if a point is inside a triangle or outside?

Posted 13 May 2014 - 07:08 AM

Hi,

I might not be a math wizard, but I think there is no simple method to do so just by calculating 2 point distance. But those "heavy math" are not so heavy in my opinion, because you can study their steps and see its example code. For example I found these websites which also has code example:

http://www.geeksforg...riangle-or-not/
http://totologic.blo...angle-test.html

So I think you just might have to do a little study and reading.
Was This Post Helpful? 1
  • +
  • -

#4 Hramyzn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-April 14

Re: How to tell if a point is inside a triangle or outside?

Posted 13 May 2014 - 12:23 PM

View Posttarmizi_adam2005, on 13 May 2014 - 07:08 AM, said:

Hi,

I might not be a math wizard, but I think there is no simple method to do so just by calculating 2 point distance. But those "heavy math" are not so heavy in my opinion, because you can study their steps and see its example code. For example I found these websites which also has code example:

http://www.geeksforg...riangle-or-not/
http://totologic.blo...angle-test.html

So I think you just might have to do a little study and reading.


Well, thanks. I guess I'll have to do it the mathematical way :/
It's not that the math is really hard to understand, but I always try not to use anything other than basic math in programming. Kinda takes away from the fun of solving problems.
Was This Post Helpful? 0
  • +
  • -

#5 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 323
  • Joined: 12-January 11

Re: How to tell if a point is inside a triangle or outside?

Posted 13 May 2014 - 12:33 PM

This is a really good explanation of how to determine if a point is inside a triangle. Give it a try.

Quote

I always try not to use anything other than basic math in programming. Kinda takes away from the fun of solving problems.


I completely disagree. While it makes the programming harder to understand for the less math centric programmer, complex algorithms discovered by people who are way smarter than me make a sometimes lengthy process into something that takes only a few milliseconds. I would not discount a given algorithm because it is not "basic math". This is especially important when you have to worry about things such as graphics plotting and lag.
Was This Post Helpful? 0
  • +
  • -

#6 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 215
  • View blog
  • Posts: 1,046
  • Joined: 07-February 14

Re: How to tell if a point is inside a triangle or outside?

Posted 13 May 2014 - 01:04 PM

Math is very crucial in programming, not something to skip. You can reduce heavy procedures with simple formulas.

As the link provided learn to make simple functions. In this case a bool return should suffice.

a general prototype:
 struct POINT{
    int x;
    int y;
}

bool isInShape( POINT A,  POINT * shape, size_t numPts);  






You should also realize that computers is discretely segmented. Like for pixels in a picture. The definition of a line that create lines, may be mathematical beyond a purist math region of the area.
This is the most common line algorithm I think.
http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1