Hi Hops. I’ve been working on a way to replace the When (self) is Tapped/Pressed in my Rubik’s Cube project with a single conditional Rule (since the native rule is flawed). The project uses triangles to render each face, but astute observers will also recognize that each face is also a parallelogram. I’ll resist the temptation to mention anything more on how I solved the problem as I thought this might make an interesting challenge for mathematically minded forumers (and perhaps a method with a shorter equation will be shown). So here it is:

**Part 1**

Given:

- A point (x, y)
- A triangle with vertices {(x1,y1),(x2,y2),(x3,y3)}

Find:

- A single logical expression that returns TRUE when the point is inside the triangle

**Part 2**

Given:

- A point (x, y)
- A parallelogram with vertices {(x1,y1),(x2,y2),(x3,y3),(x4,y4)}
- Vertices 1 & 3 are diagonally across
- Vertices 2 & 4 are diagonally across

Find:

- A single logical expression that returns TRUE when the point is inside the parallelogram

**Bonus Credit**

Make a Hopscotch project(s) to demonstrate your Rule

*The solution to these problems can also be used for collision detection if the object doesn’t fit nicely in a square or circle!*

[Edit]

I’ll update this if a shorter solution is found. So far the shortest (that is, it has the fewest operations or Hopscotch code blocks) is:

## Cross product direction

Cross product can be used to determine the direction of the normal vector.

Consider PA x PB (that’s PA cross PB) for a point P inside the parallelogram:

Using your *right* hand & following the arrow, curl your fingers from A to B. Your thumb will point *outward* indicating a positive direction to the normal vector

Now consider PA x PB (PA cross PB) for a point P outside the parallelogram:

Again, using your *right* hand & following the arrow, curl your fingers from A to B. Your thumb will point *inward* indicating a negative direction to the normal vector.

For a parallelogram, if all four cross products are positive then the point P is inside the triangle.

```
When
(Ax-Px)(By-Py) - (Ay-Py)(Bx-Px) >= 0
and
(Bx-Px)(Cy-Py) - (By-Py)(Cx-Px) >= 0
and
(Cx-Px)(Dy-Py) - (Cy-Py)(Dx-Px) >= 0
and
(Dx-Px)(Ay-Py) - (Dy-Py)(Ax-Px) >= 0
```

For a triangle, there’s only three cross products to check to determine if the point P is inside the triangle.

```
When
(Ax-Px)(By-Py) - (Ay-Py)(Bx-Px) >= 0
and
(Bx-Px)(Cy-Py) - (By-Py)(Cx-Px) >= 0
and
(Cx-Px)(Ay-Py) - (Cy-Py)(Ax-Px) >= 0
```