def test_parabola_intersection(): l1 = Line(Point(1, -2), Point(-1, -2)) l2 = Line(Point(1, 2), Point(-1, 2)) l3 = Line(Point(1, 0), Point(-1, 0)) p1 = Point(0, 0) p2 = Point(0, -2) p3 = Point(120, -12) parabola1 = Parabola(p1, l1) # parabola with parabola assert parabola1.intersection(parabola1) == [parabola1] assert parabola1.intersection(Parabola( p1, l2)) == [Point2D(-2, 0), Point2D(2, 0)] assert parabola1.intersection(Parabola(p2, l3)) == [Point2D(0, -1)] assert parabola1.intersection(Parabola(Point(16, 0), l1)) == [Point2D(8, 15)] assert parabola1.intersection(Parabola(Point( 0, 16), l1)) == [Point2D(-6, 8), Point2D(6, 8)] assert parabola1.intersection(Parabola(p3, l3)) == [] # parabola with point assert parabola1.intersection(p1) == [] assert parabola1.intersection(Point2D(0, -1)) == [Point2D(0, -1)] assert parabola1.intersection(Point2D(4, 3)) == [Point2D(4, 3)] # parabola with line assert parabola1.intersection(Line(Point2D(-7, 3), Point( 12, 3))) == [Point2D(-4, 3), Point2D(4, 3)] assert parabola1.intersection(Line(Point(-4, -1), Point(4, -1))) == [Point(0, -1)] assert parabola1.intersection(Line(Point(2, 0), Point(0, -2))) == [Point2D(2, 0)] raises( TypeError, lambda: parabola1.intersection(Line(Point(0, 0, 0), Point(1, 1, 1)))) # parabola with segment assert parabola1.intersection(Segment2D( (-4, -5), (4, 3))) == [Point2D(0, -1), Point2D(4, 3)] assert parabola1.intersection(Segment2D((0, -5), (0, 6))) == [Point2D(0, -1)] assert parabola1.intersection(Segment2D((-12, -65), (14, -68))) == [] # parabola with ray assert parabola1.intersection(Ray2D( (-4, -5), (4, 3))) == [Point2D(0, -1), Point2D(4, 3)] assert parabola1.intersection(Ray2D( (0, 7), (1, 14))) == [Point2D(14 + 2 * sqrt(57), 105 + 14 * sqrt(57))] assert parabola1.intersection(Ray2D((0, 7), (0, 14))) == [] # parabola with ellipse/circle assert parabola1.intersection(Circle( p1, 2)) == [Point2D(-2, 0), Point2D(2, 0)] assert parabola1.intersection(Circle( p2, 1)) == [Point2D(0, -1), Point2D(0, -1)] assert parabola1.intersection(Ellipse( p2, 2, 1)) == [Point2D(0, -1), Point2D(0, -1)] assert parabola1.intersection(Ellipse(Point(0, 19), 5, 7)) == [] assert parabola1.intersection(Ellipse((0, 3), 12, 4)) == \ [Point2D(0, -1), Point2D(0, -1), Point2D(-4*sqrt(17)/3, Rational(59, 9)), Point2D(4*sqrt(17)/3, Rational(59, 9))] # parabola with unsupported type raises(TypeError, lambda: parabola1.intersection(2))
def test_all_circles_two_points(p1, p2): """ Check whether `all_circles` yields all expected circles with two points """ assume(p1 != p2) two_circles = EuclideanWorld.all_circles([p1, p2]) assert len(two_circles) == 2 r = p1.distance(p2) assert Circle(p1, r) in two_circles assert Circle(p2, r) in two_circles
def test_geometry(): p1 = Point(1, 2) p2 = Point(2, 3) p3 = Point(0, 0) p4 = Point(0, 1) for c in ( GeometryEntity, GeometryEntity(), Point, p1, Circle, Circle(p1, 2), Ellipse, Ellipse(p1, 3, 4), Line, Line(p1, p2), LinearEntity, LinearEntity(p1, p2), Ray, Ray(p1, p2), Segment, Segment(p1, p2), Polygon, Polygon(p1, p2, p3, p4), RegularPolygon, RegularPolygon(p1, 4, 5), Triangle, Triangle(p1, p2, p3), ): check(c, check_attr=False)
def all_circles(points): """ Find all circles the can be made using the given points as centre and "radius" """ for centre in points: radii = set( centre.distance(other) for other in points if other != centre) for radius in radii: yield Circle(centre, radius)
def test_geometry(): p1 = Point(1, 2) p2 = Point(2, 3) p3 = Point(0, 0) p4 = Point(0, 1) for c in (GeometryEntity, GeometryEntity(), Point, p1, Circle, Circle(p1, 2), Ellipse, Ellipse(p1, 3, 4), Line, Line(p1, p2), LinearEntity, LinearEntity(p1, p2), Ray, Ray(p1, p2), Segment, Segment(p1, p2), Polygon, Polygon(p1, p2, p3, p4), RegularPolygon, RegularPolygon(p1, 4, 5), Triangle): # XXX: Instance of Triangle hangs because of hasattr in check(). # Triangle(p1,p2,p3) check(c) pass
def random_lattice_neighbors(N): ''' Generate the neighbor dict for random lattice interactions constructed using the Voronoi/Delaunay prescription. This requires a lot of geometry and therefore is likely computationaly costly. Parameters: N: The number of ising sites. ''' x_coords = np.random.rand(N) y_coords = np.random.rand(N) neigh_dict = dict() # Initialize every dictionary entry to an empty set for n in range(N): neigh_dict[n] = set() # Iterate through every possible combination of three points to determine # if the circle they create contains no points. for comb in list(combinations(range(N), 3)): # Extract points in circle x1, y1 = x_coords[comb[0]], y_coords[comb[0]] x2, y2 = x_coords[comb[1]], y_coords[comb[1]] x3, y3 = x_coords[comb[2]], y_coords[comb[2]] # Calculate the center and radius of the circle defined by the # three points. try: c = Circle(Point(x1, y1), Point(x2, y2), Point(x3, y3)) except GeometryError: continue c_x, c_y = c.center c_x = float(c_x) c_y = float(c_y) r = float(c.radius) # Run through all the points and see if any are within the circle pt_in_circ = False for n in range(N): x, y = x_coords[n], y_coords[n] if (c_x - x)**2 + (c_y - y)**2 < r**2 - 1e-8: pt_in_circ = True break if pt_in_circ == False: neigh_dict[comb[0]].update(set([comb[1], comb[2]])) neigh_dict[comb[1]].update(set([comb[0], comb[2]])) neigh_dict[comb[2]].update(set([comb[0], comb[1]])) return neigh_dict, x_coords, y_coords
def test_all_circles_two_points(p1, p2, p3): """ Check whether `all_circles` yields all expected circles with three points """ assume(p1 != p2) assume(p2 != p3) assume(p3 != p1) two_circles = list(EuclideanWorld.all_circles([p1, p2, p3])) assert len(two_circles) <= 6 assert len(two_circles) >= 3 assert Circle(p1, p1.distance(p2)) in two_circles assert Circle(p1, p1.distance(p3)) in two_circles assert Circle(p2, p2.distance(p1)) in two_circles assert Circle(p2, p2.distance(p3)) in two_circles assert Circle(p3, p3.distance(p1)) in two_circles assert Circle(p3, p3.distance(p2)) in two_circles
from sympy.geometry.ellipse import Circle from sympy.geometry.polygon import Point, Polygon, Triangle import numpy as np from scipy.optimize import fsolve from sympy.solvers import solve from sympy import Symbol from sympy.solvers import linsolve from sympy.geometry import Line from sympy import Eq import matplotlib.pyplot as plt import matplotlib.patches as mpatches from matplotlib.collections import PatchCollection c1 = Circle(Point(0, 0), 0.08462309706809855) c2 = Circle(Point(1, 0), 0.5516666664845306) c3 = Circle(Point(0, 1), 0.5778696787291522) x = Symbol('x') y = Symbol('y') f = solve(c1.equation(x, y) - c2.equation(x, y), [x, y], dict=True) print(f[0][x].evalf(5)) #l = Line(Eq(y - 0.15, 0)) #print(l.equation()) # o = c2.intersection(c3) # print(o.) # print(inters) a = tuple(Point(0.14, 0.15).evalf(4)) print(a) p1, p2, p3 = Point(0.35, 0.335), Point(0.35, 0.34), Point(0.355, 0.34)