Math 2D ------------------------------------------------------ General notes: theta_radians is an angle between 0 and 2PI. sqr means sqaure e.g. sqr(5) = 5 * 5 = 25 sqrt means square-root, e.g. sqrt(25) = 5 ------------------------------------------------------ Calculate the distance between two points: /// /// Get the distance (direct line of sight) between (x1, y1) and (x2, y2) /// double GetDistance(const double x1, const double y1, const double x2, const double y2) { const double delta_x = x2 - x1 const double delta_y = y2 - y1; const double distance = sqrt(sqr(distance) + sqr(distance)) return distance; } ------------------------------------------------------ Rotate a point around an ellipse/circle from the center point: const double edge_x = center_x + cos(theta_radians) * radius_width; const double edge_y = center_y + sin(theta_radians) * radius_height; (theta_radians is an angle between 0 and 2PI) ------------------------------------------------------ Calculate the edge point of an ellipse using the angle: const double c = cos(theta_radians); const double s = sin(theta_radians); const double edge_x = (radius_width * radius_height * c) / sqrt(sqr(radius_height * c) + sqr(radius_width * s)); const double edge_y = (radius_width * radius_height * s) / sqrt(sqr(radius_height * c) + sqr(radius_width * s)); ------------------------------------------------------ Convert a radian into a degree: double ToDegrees(const double radians) { return (radians * 180.0) / PI; } ------------------------------------------------------ Convert a degree into a radian: double ToRadians(const double degrees) { return (PI / 180.0) * degrees; } ------------------------------------------------------ Get whether a point is within a line: /// /// Find out whether a given point (x, y) is within a line (x1, y1) -> (x2, y2) /// bool IsWithinLine(const double x1, const double x1, const double x2, const double y2, const double x, const double y) { bool within_line = false; const double delta_x = x2 - x1; if (delta_x == 0) { if (x == x1) { const double miny = min(y1, y2); const double maxy = max(y1, y2); if (y >= miny && y <= maxy) { within_line = true; } } } else { const double delta_y = y2 - y1; const double m = abs(delta_y) / abs(delta_x); const double b = y1 - (m * x1); if (y == round((m * x) + b)) { within_line = true; } } return within_line; } ------------------------------------------------------ Get the angle of a line from the horizontal line: /// /// Get the angle of a line (x1, y1) -> (x2, y2) against the horizontal line /// double GetLineAngle(const double x1, const double y1, const double x2, const double y2) { double theta_radians = 0; const double delta_x = x2 - x1; if (delta_x == 0) { if (y2 > y1) { theta_radians = 0; } else { theta_radians = PI; } } else { const double delta_y = y2 - y1; theta = atan(delta_y / delta_x); if ( p2x > p1x ) { theta_radians = PI / 2.0 - theta; } else { theta_radians = PI * 1.5 - theta; } } return theta_radians; } ------------------------------------------------------