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

Page 1 of 1

## 5 Replies - 1422 Views - Last Post: 13 May 2014 - 01:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=346859&amp;s=85139cf4146f3b15d7a2a9b1e45b6a5e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Hramyzn

Reputation: 0
• 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

Reputation: 0
• 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.

• جوروترا

Reputation: 283
• Posts: 979
• 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.

### #4 Hramyzn

Reputation: 0
• 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

tarmizi_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.

### #5 Psyguy

• D.I.C Regular

Reputation: 81
• Posts: 349
• 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.

### #6 infernorthor

• D.I.C Lover

Reputation: 362
• Posts: 1,718
• 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