Basic geometry functions
In this article we will see how to implement some basic geometry functions in 2D space. We will wirte line segment intersection and circumcircle.
Let's start with writing some basic functions. Then we will write next, more complexed functions. We can write them as static methods of Geometry class.
Distance between two points
We will count it using Pythagorean theorem ($c = \sqrt{a\times a + b \times b}$).
Code
public static function Distance(a, b):Number
{
return(Math.sqrt( (b.x-a.x)*(b.x-a.x) + (b.y-a.y)*(b.y-a.y) ));
}
If a point is inside of a rectangle
Let's have rectangle, which is defined by top left corner and bottom right corner ("b" a "c"). We will ask, whether point "a" lies inside the rectengle. a.x must lie between b.x and c.x, same for a.y.
Code
public static function InRect(a, b, c):Boolean
{
if(a.x >= Math.min(b.x, c.x) && a.x <= Math.max(b.x, c.x)
&& a.y >= Math.min(b.y, c.y) && a.y <= Math.max(b.y, c.y))
return true;
return false;
}
Intersection of two line segments
Let's have line segments "a" and "b" defined by two points (starting points "a1", "b1", and ending points "a2", "b2"). We want to know, whether they intersect each other. Firstly, we find the intersection of lines "a", "b". Then we check, whether that intersection point is located between starting and ending point of each line.
I use this formula for computing intersection. It is based on determinants.
Code
public static function GetLineIntersection(a1, a2, b1, b2):Point
{
var dax = (a1.x-a2.x), dbx = (b1.x-b2.x);
var day = (a1.y-a2.y), dby = (b1.y-b2.y);
var Den = dax*dby - day*dbx;
if (Den == 0) return null; // parallel
var A = (a1.x * a2.y - a1.y * a2.x);
var B = (b1.x * b2.y - b1.y * b2.x);
var I = new Point(0,0);
I.x = ( A*dbx - dax*B ) / Den;
I.y = ( A*dby - day*B ) / Den;
if(InRect(I, a1, a2) && InRect(I, b1, b2)) return I;
return null;
}
Circumcenter
Let us have three points (a, b, c). We are looking for a circle (center and radius), which intersects all three points. This circumcenter is the intersection of normals (a,b) and (b,c). Radius is distance between circumcenter and a (or b, or c).
Code
public static function GetCircumcenter(a, b, c):Point
{
// m1 - center of (a,b), the normal goes through it
var f1 = (b.x - a.x) / (a.y - b.y);
var m1 = new Point((a.x + b.x)/2, (a.y + b.y)/2);
var g1 = m1.y - f1*m1.x;
var f2 = (c.x - b.x) / (b.y - c.y);
var m2 = new Point((b.x + c.x)/2, (b.y + c.y)/2);
var g2 = m2.y - f2*m2.x;
// degenerated cases
// - 3 points on a line
if (f1 == f2) return null;
// - a, b have the same height -> slope of normal of |ab| = infinity
else if(a.y == b.y) return new Point(m1.x, f2*m1.x + g2);
else if(b.y == c.y) return new Point(m2.x, f1*m2.x + g1);
var x:Number = (g2-g1) / (f1 - f2);
return new Point(x, f1*x + g1);
}
As a math teacher I have found these math Java applets by Japanese
teachers useful:
http://www.ies-math.com/math/java/
Suggest rewriting a few in HTML5 and offer that website a business
deal to market them. Or, do inspired imitations.
My Chromebook and those of thousands of students will not
do Java so there is a market.
Cam Trenor
April 7th, 2013secondary math/science
Bellevue, Wa.
Awesome! Thanks!
November 14th, 2013VaRtWQQ6A8z
February 21st, 2025mI6dWOb7zQY
February 21st, 2025N0UxTqUI4y6
February 21st, 2025Zyj3Y4QF914
February 21st, 2025ruYUCPucusr
February 21st, 2025o4cToi3i51P
February 21st, 2025ImkjN8ed4wI
February 21st, 2025cfYjdRt5qFX
February 21st, 20253iSgFEBjZJz
February 21st, 2025egBFOknIiAK
February 21st, 2025OYYqZqwCnjz
February 21st, 2025QZJTml5mx0J
February 21st, 2025B2K4FNi8347
February 21st, 2025wjafAjYC1UQ
February 21st, 2025actUPFy7EAh
February 21st, 2025JgmcZzu3mVW
February 21st, 2025fvkNHiTiqcd
February 21st, 2025hKyrQott2MV
February 21st, 2025ZYjyE483Blu
February 21st, 2025OnNcFPUJ8Qw
February 21st, 2025x0M6hcgAZ5E
February 21st, 2025uHrl4xvvBr7
February 21st, 20251cL7PnzYq2q
February 21st, 20250TtD6xXdm4N
February 21st, 2025xnW0ijqZyT9
February 21st, 2025nzZYJk78GiI
February 21st, 2025x52cFhrjxgb
February 21st, 2025wVdhxh6LGx6
February 21st, 2025HfIFsac2c2t
February 21st, 2025L8UJnncfMyc
February 21st, 20259WzB43EDvCp
February 21st, 2025xKotRCjzWKn
February 21st, 2025veB3qFThmxW
February 21st, 2025HKUaXSqIt1J
February 21st, 2025bkw1D2hIpCJ
February 21st, 2025