Skip to content

Instantly share code, notes, and snippets.

@jeremyBanks
Created September 14, 2008 20:50
Show Gist options
  • Save jeremyBanks/10763 to your computer and use it in GitHub Desktop.
Save jeremyBanks/10763 to your computer and use it in GitHub Desktop.
[2010-01] finding the point to make an equilaterial trangle with a line segment, i was figuring out for fractical triangle khoh curve or whatever
"""
The equation of the line segment near A is
y = a.y + (x - a.x) * slopeA
The equation of the line segment near B is
y = b.y + (x - b.x) * slopeB
Isolate x in the system;
a.y + (x - a.x) * slopeA = b.y + (x - b.x) * slopeB
(x - a.x) * slopeA - (x - b.x) * slopeB = b.y - a.y
x * slopeA - a.x * slopeA - x * slopeB + b.x * slopeB = b.y - a.y
x * slopeA - x * slopeB = b.y - a.y - b.x * slopeB + a.x * slopeA
x (slopeA - slopeB) = b.y - a.y - b.x * slopeB + a.x * slopeA
x = (b.y - a.y - b.x * slopeB + a.x * slopeA) / (slopeA - slopeB)
There.
"""
x = (b.y - a.y - b.x * slopeB + a.x * slopeA) / (slopeA - slopeB)
y = a.y + (x - a.x) * slopeA
def equalaterize(a, b):
"""Given two points return the third point
needed to form an equalaterial triangle.
Two possible points fit this criteria, returned is
the one that would be above the two points if they
were drawn on a vertical line with a to the left."""
distance = a.distanceFrom(b)
dX = b.x - a.x
dY = b.y - a.y
if dY:
lineSlope = (b.x - a.x) / (b.y - a.y)
lineAngle = math.asin(lineSlope)
else:
log.info("Points are vertically aligned, angle is 0°.")
lineAngle = 0
angleA = (lineAngle + 60) % 360
if angleA: slopeA = math.sin(angleA)
angleB = (lineAngle - 60) % 360
if angleB: slopeB = math.sin(angleB)
if angleA == 0:
return Point(a.x, b.y + dX * slopeB)
elif angleB == 0:
return Point(b.x, a.y - dX * slopeA)
else:
"""
The equation of the line segment near A is
y = a.y + (x - a.x) * slopeA
The equation of the line segment near B is
y = b.y + (x - b.x) * slopeB
Isolate x in the system;
a.y + (x - a.x) * slopeA = b.y + (x - b.x) * slopeB
(x - a.x) * slopeA - (x - b.x) * slopeB = b.y - a.y
x * slopeA - a.x * slopeA - x * slopeB + b.x * slopeB = b.y - a.y
x * slopeA - x * slopeB = b.y - a.y - b.x * slopeB + a.x * slopeA
x (slopeA - slopeB) = b.y - a.y - b.x * slopeB + a.x * slopeA
x = (b.y - a.y - b.x * slopeB + a.x * slopeA) / (slopeA - slopeB)
There.
"""
x = (b.y - a.y - b.x * slopeB + a.x * slopeA) / (slopeA - slopeB)
y = a.y + (x - a.x) * slopeA
return Point(x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment