Get into GameDev:
cheat-codes for gamedev interviews
━ Maths - Polygons & Polyhedrons ━
Introduction
Polygons
Polygons are 2D shapes. A triangle is a polygon with 3 sides, a rectangle is a polygon with 4 sides, and so on. Remember that it is often useful to think of polygons as restricted planes. May problems, such as "does the line intersect the polygon", are easy to solve if we simplify them to "does the line intersect the plane which contains the polygon" and then we can constrain the problem to ask "given the intersection on the polygon's plane, does it lie within the polygon's actual bounds?"
Practice Question: Is point M in the rectangle defined by points A, B, C, and D?
Answer: The first approach I will outline is geometric. It's simple to conceptualize but it's performance is slow. Imagine M indeed lies within rectangle ABCD. If that is the case, then when considered with the rectangles vertices, we can form 4 triangles: ABM, BMC, DPC, and APD. Sum the area of each triangle and compare it to the rectangle ABCD's total area. If the sum of triangle areas is greater than the rectangle's total area, then M must be outside of the rectangle. If the sums are instead equal, then we check if one triangle had zero area which would indicate that M lies on an edge of ABCD. You can generalize this approach to any convex polygon for which you can easily calculate its area, the number of triangles is just the number of sides.
The second approach we will cover is again geometric. In this approach we will project the point onto two side of the rectangle. Specifically two adjacent sides. Because a rectangle is comprised only of right angles, we know that the four sides will have two pairs of matching length sides. Four sides: two distinct lengths. Using this symmetry, we realize that we only need to project onto one side of each length which is why we can get away with only two projections.
https://stackoverflow.com/questions/2752725/finding-whether-a-point-lies-inside-a-rectangle-or-not
Answer: The first approach I will outline is geometric. It's simple to conceptualize but it's performance is slow. Imagine M indeed lies within rectangle ABCD. If that is the case, then when considered with the rectangles vertices, we can form 4 triangles: ABM, BMC, DPC, and APD. Sum the area of each triangle and compare it to the rectangle ABCD's total area. If the sum of triangle areas is greater than the rectangle's total area, then M must be outside of the rectangle. If the sums are instead equal, then we check if one triangle had zero area which would indicate that M lies on an edge of ABCD. You can generalize this approach to any convex polygon for which you can easily calculate its area, the number of triangles is just the number of sides.
The second approach we will cover is again geometric. In this approach we will project the point onto two side of the rectangle. Specifically two adjacent sides. Because a rectangle is comprised only of right angles, we know that the four sides will have two pairs of matching length sides. Four sides: two distinct lengths. Using this symmetry, we realize that we only need to project onto one side of each length which is why we can get away with only two projections.
https://stackoverflow.com/questions/2752725/finding-whether-a-point-lies-inside-a-rectangle-or-not
An unsigned int-32 would have all 32 bits to represent its value, but we need to remember to subtract one because one combination of bits is used to represent 0. Therefore the max value of a uint-32 is 2^32-1.
But I asked for a signed int-32 which requires the first bit to represent the sign. Therefore the max value of a uint-32 is 2^31-1.
Additional reading:
https://stackoverflow.com/questions/94591/what-is-the-maximum-value-for-an-int32
Additional reading:
https://stackoverflow.com/questions/94591/what-is-the-maximum-value-for-an-int32
Example
X
Practice Question: Example Question?
An unsigned int-32 would have all 32 bits to represent its value, but we need to remember to subtract one because one combination of bits is used to represent 0. Therefore the max value of a uint-32 is 2^32-1.
But I asked for a signed int-32 which requires the first bit to represent the sign. Therefore the max value of a uint-32 is 2^31-1.
Additional reading:
https://stackoverflow.com/questions/94591/what-is-the-maximum-value-for-an-int32
Additional reading:
https://stackoverflow.com/questions/94591/what-is-the-maximum-value-for-an-int32
Example
X
Practice Question: Example Question?
An unsigned int-32 would have all 32 bits to represent its value, but we need to remember to subtract one because one combination of bits is used to represent 0. Therefore the max value of a uint-32 is 2^32-1.
But I asked for a signed int-32 which requires the first bit to represent the sign. Therefore the max value of a uint-32 is 2^31-1.
Additional reading:
https://stackoverflow.com/questions/94591/what-is-the-maximum-value-for-an-int32
Additional reading:
https://stackoverflow.com/questions/94591/what-is-the-maximum-value-for-an-int32