Skip to content

Instantly share code, notes, and snippets.

@vyuh
Created April 2, 2015 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vyuh/fdeedbd88dce31944363 to your computer and use it in GitHub Desktop.
Save vyuh/fdeedbd88dce31944363 to your computer and use it in GitHub Desktop.
Antialiased Geometric constructions
#-------------------------------------------------------------------------------
# Name: geometry_pk
# Purpose: Simple approach to implementing antialiased graphics.
#
# Author: pk
#
# Created: 30-03-2015
# Copyright: (c) pk 2015
# Licence: ISC
#-------------------------------------------------------------------------------
import math
class Point(object):
def __init__(self, x, y):
self.x = float(x)
self.y = float(y)
def __str__(self):
return "<Point: ({0}, {1})>".format(self.x, self.y)
def distance_from(self, point):
return pow(pow(self.x-point.x, 2) + pow(self.y-point.y, 2), 0.5)
import math
def ints_between(a, b):
(a, b) = sorted([a, b])
return list(range(math.floor(a+1), math.ceil(b)))
class LineSegment(object):
def __init__(self, sun, eye):
(self.sun, self.eye) = (sun, eye) if sun.x <= eye.x else (eye, sun)
def __str__(self):
return "<LineSegment: {0} to {1}>".format(self.sun, self.eye)
def contrib_under_me(self):
return (self.eye.x - self.sun.x)*(self.sun.y+self.eye.y)/2
def x(self, y):
cache = self.eye.y-self.sun.y
if math.fabs(cache) != 0:
ret = self.sun.x+(self.eye.x-self.sun.x)*(float(y)-self.sun.y)/cache
else:
ret= None
return ret
def critical_abassicae(self):
a=[self.sun.x, self.eye.x]
a.extend(ints_between(*a))
a.extend([self.x(y) for y in ints_between(*sorted([self.sun.y, self.eye.y]))])
return sorted(list(set(a)))
# class LineSegment(object):
def main():
po = Point(0, 0)
mo = Point(-4, 3)
print(po)
print(po.distance_from(po))
ls = LineSegment(mo, po)
print(ls)
print(ls.contrib_under_me())
print(ls.critical_abassicae())
pass
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment