Files @ 7c268e382b96
Branch filter:

Location: OneEye/exp/geometry.py

Laman
geometry: rewritten Line
import math

from analyzer.epoint import EPoint


class Line:
	def __init__(self,alpha,d):
		self._alpha=alpha
		self._d=d
		self._sin=math.sin(alpha)
		self._cos=math.cos(alpha)

	@staticmethod
	def fromNormal(a,b,c):
		"""ax + by + c = 0"""
		norm=-c/abs(c)*math.sqrt(a**2+b**2)
		(a_,b_,c_)=(a/norm,b/norm,c/norm)
		alpha=math.acos(a_) if b_>=0 else 2*math.pi-math.acos(a_)
		return Line(alpha,-c_)

	@staticmethod
	def fromPoints(a,b):
		return Line.fromNormal(a.y-b.y, b.x-a.x, (b.y-a.y)*a.x+(a.x-b.x)*a.y)

	def toNormal(self):
		# https://en.wikipedia.org/wiki/Line_(mathematics)#In_normal_form
		"""ax + by + c = 0"""
		return (self._cos, self._sin, -self._d)

	def intersect(self,line):
		if self._alpha==line._alpha: return None
		(a,b,c)=self.toNormal()
		(d,e,f)=line.toNormal()
		x=(b*f-c*e)/(a*e-b*d)
		y=(c*d-a*f)/(a*e-b*d)
		return EPoint(x,y)

	def distanceTo(self,point):
		# https://en.wikipedia.org/wiki/Point-line_distance#Line_defined_by_an_equation
		(a,b,c)=self.toNormal()
		return abs(a*point.x+b*point.y+c) # a**2 + b**2 == 1 for Hesse normal form

	def __str__(self): return "({0},{1})".format(self._alpha,self._d)
	def __repr__(self): return "Line({0},{1})".format(repr(self._alpha),repr(self._d))


def angleDiff(alpha,beta):
	diff=abs(alpha-beta)
	if diff>math.pi: diff=2*math.pi-diff
	return diff