Skip to content

Instantly share code, notes, and snippets.

@ajweeks
Last active January 31, 2016 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajweeks/f95b8125a385a2afc0eb to your computer and use it in GitHub Desktop.
Save ajweeks/f95b8125a385a2afc0eb to your computer and use it in GitHub Desktop.
Devember Ninth

Devember Day 09

Today was another easy day do get the hour of coding done on. However I'm going to have to keep this entry short, I don't have a ton of free time tonight.

I got a solid hour and three quarters of programming in today, during class. Nothing too difficult, but still kind of interesting work. This week's "theme" if you will, is classes. So in this exercise we had to make a Fraction class, a Light class, and a Cone class. We also had to implement some basic point in shape checking, for seeing when the mouse was clicked in a 'light' or when it was hovering over a triangle.

We are supposed to use the game engine's physics system to check if a point is inside each triangle, but I wanted to know how it's actually done so I implemented the Barycentric coordinate system. When I say that I implemented it, all I really mean is that I copied straight from a stack overflow answer and modified it to fit my variables. I don't quite understand how it works, but it's cool that it does. Kind of like quantum computing, but probably a lot less complicated. Maybe I'll look into it a bit more later.

The following is the code that checks if a point, p is inside a triangle with points p1, p2, p3. I believe that it it only works if the points are given in an anti-clockwise direction.

double area = 1.0 / 2.0 * (-p1.y*p2.x + p0.y*(-p1.x + p2.x) + p0.x*(p1.y- p2.y) + p1.x*p2.y);

double s = 1.0 / (2 * area)*(p0.y*p2.x - p0.x*p2.y + (p2.y - p0.y)*p.x + (p0.x - p2.x)*p.y);
double t = 1.0 / (2 * area)*(p0.x*p1.y - p0.y*p1.x + (p0.y - p1.y)*p.x + (p1.x - p0.x)*p.y);

bool isInside = (s > 0 && t > 0 && (1 - s - t) > 0);

There are a few more exercises we have to do this week, I'll probably finish them off over the next few days.

The final result of the assignment: (while hovering over the green triangle)

Previous Entry | All Entries | Next Entry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment