def __init__(self, size, loc=None, color='#2f2f2f'): self.size = size self.color = color self.loc = loc if loc is not None else Vector2(0, 0) self.down = Vector2(0, self.size) self.right = Vector2(self.size, 0) super(PlusView, self).__init__()
def test_case(msg, seg1, seg2, ray1, ray2, crosses, cross_point): global num_fails report = "" def error_out(): return "seg1 = {}\nseg2 = {}\nray tip = {}\nray target = {}".format( seg1, seg2, ray1, ray2) passed = True report_results = "" # report_results += " segment_crosses_ray: " # try: # crosses_result = Vector2.segment_crosses_ray(seg1, seg2, ray1, ray2) # report_results += str(crosses_result) + "\n" # if crosses_result != crosses: # passed = False # report_results = report_results[:-1] + " (must be {})\n".format(crosses) # except StandardError as er: # report_results += "ERROR: {}".format(er) + "\n" # passed = False report_results += "where_segment_crosses_ray: " try: cross_point_result = Vector2.where_segment_crosses_ray( seg1, seg2, ray1, ray2) report_results += str(cross_point_result) + "\n" if not vector_equal(cross_point_result, cross_point): passed = False report_results = report_results[:-1] + " (must be {})\n".format( cross_point) except StandardError as er: report_results += "ERROR: {}".format(er) + "\n" passed = False # make report report += "------------------------------------------------------------------------------\n" report += msg + "\n" if not passed: num_fails += 1 report += "FAIL\n" report += error_out() + "\n" else: report += "OK\n" report += report_results report += "------------------------------------------------------------------------------\n\n" print report
def offset_by(self, vec): ''' move all coordinates in world_vertices by vec ''' for vnum in range(len(self.world_vertices) / 3): vhead = vnum * 3 old_vec = Vector2(self.world_vertices[vhead], self.world_vertices[vhead + 1]) self.set_vector(vnum, old_vec + vec)
def find_corners(self): wd = self.width axis = self.v2 - self.v1 perp = Vector2(axis.y, -axis.x) perp /= perp.length() c1 = self.v1 + wd * perp c2 = self.v1 - wd * perp c3 = self.v2 - wd * perp c4 = self.v2 + wd * perp return c1, c2, c3, c4
print report # seg1 = Vector2(0.75, 1.0) # seg2 = Vector2(0.5, 0.5) # tip = Vector2(0.5, 0.5) # left = Vector2(-0.3, 1.3) # right = Vector2(1.3, 1.3) # print(Vector2.where_segment_crosses_ray(seg1, seg2, tip, left)) # print(Vector2.where_segment_crosses_ray(seg1, seg2, tip, right)) test_case( "check what happens when segment lies on the ray behind the tip:", ray1=Vector2(0.5, 0.5), # tip ray2=Vector2(1.5, 1.5), # target seg1=Vector2(0.15, 0.15), seg2=Vector2(0.45, 0.45), crosses=False, cross_point=None) test_case( "check what happens when segment lies on the ray behind the tip (seg flipped):", ray1=Vector2(0.5, 0.5), # tip ray2=Vector2(1.5, 1.5), # target seg1=Vector2(0.45, 0.45), seg2=Vector2(0.15, 0.15), crosses=False, cross_point=None)
from mesh2d import Mesh2d, Vector2 def poly_repr(poly): outl = poly.outline_coordinates() crds = list( (outl[2 * pos], outl[2 * pos + 1]) for pos in range(len(outl) / 2)) st = '' st += 'indices: ' + repr(poly.outline) st += '\n' st += 'outline: ' + repr(crds) return st a = Vector2(0., 0.) b = Vector2(1., 0.) c = Vector2(1., 1.) d = Vector2(0., 1.) i1 = Vector2(0.5, 0.25) i2 = Vector2(0.75, 0.5) i3 = Vector2(0.5, 0.75) i4 = Vector2(0.25, 0.5) vertices = [a, b, c, d] poly = Mesh2d(vertices[:], range(len(vertices))) print(poly_repr(poly)) print(
import os import sys here = os.path.abspath(os.path.dirname(__file__)) module_root = os.path.join(here, "..") sys.path.append(module_root) from mesh2d import Vector2 as v print( v.vertex_on_ray(v(54.800000001, -36.2), v(54.8000000, -124.8), v(54.8000000, -313.0)))
def test_case(NW, SE): start = NW end = SE v0 = start v1 = Vector2(end.x, start.y) v2 = end v3 = Vector2(start.x, end.y) width = end.x - start.x height = end.y - start.y cntr_x = start.x + width / 2. cntr_y = start.y + height / 2. cntr = Vector2(cntr_x, cntr_y) sz = min(height, width) / 4. h10 = (Vector2(.1, .9) - Vector2(.5, .5)) * sz + cntr #4 h11 = (Vector2(.1, .1) - Vector2(.5, .5)) * sz + cntr #5 h12 = (Vector2(.4, .1) - Vector2(.5, .5)) * sz + cntr #6 h13 = (Vector2(.4, .9) - Vector2(.5, .5)) * sz + cntr #7 h20 = (Vector2(.9, .9) - Vector2(.5, .5)) * sz + cntr #8 h21 = (Vector2(.5, .5) - Vector2(.5, .5)) * sz + cntr #9 h22 = (Vector2(.9, .1) - Vector2(.5, .5)) * sz + cntr #10 poly = Mesh2d([v0, v1, v2, v3], range(4)) poly.add_hole([h10, h11, h12, h13]) poly.add_hole([h20, h21, h22]) ray1 = poly.vertices[10].copy() # ray2 = ray1 + Vector2(0., -1.) ray1 = Vector2(54.8, -124.8) ray2 = Vector2(54.8, -313.0) inters = poly.trace_ray(ray1, ray2) edge = inters[1] if edge != (0, 1): return False, inters return True, inters
poly.add_hole([h20, h21, h22]) ray1 = poly.vertices[10].copy() # ray2 = ray1 + Vector2(0., -1.) ray1 = Vector2(54.8, -124.8) ray2 = Vector2(54.8, -313.0) inters = poly.trace_ray(ray1, ray2) edge = inters[1] if edge != (0, 1): return False, inters return True, inters nw = Vector2(-211, -313) se = Vector2(232, 152) result, inters = test_case(nw, se) print("inters: {}, on edge {}".format(inters[0], inters[1])) # do test after test forever # rnd_size = 100. # min_size = 0.1 # start_nw = 0 # end_nw = rnd_size + min_size # count = 0
import sys import os # import pudb; pu.db here = os.path.abspath(os.path.dirname(__file__)) module_root = os.path.join(here, "..") sys.path.append(module_root) from mesh2d import Vector2 # check equality and inequality operations a = Vector2(0.5, 0.5) b = Vector2(0.5, 0.5) assert((a == b) == True) assert((a != b) == False) b = Vector2(0.5, 0.45) assert((a == b) == False) assert((a != b) == True) b = Vector2(0.45, 0.5)
import sys import os here = os.path.abspath(os.path.dirname(__file__)) module_root = os.path.join(here, "..") sys.path.append(module_root) from mesh2d import Mesh2d, Vector2 indices = [0, 1, 2, 5, 6, 3, 7, 4] vertices = [ Vector2(0., 0.), Vector2(0.5, 0.5), Vector2(1., 0.), Vector2(1., 1.), Vector2(0., 1.), Vector2(1., 0.5), Vector2(1., 0.75), Vector2(0.75, 1.), ] poly = Mesh2d(vertices, indices) # should be outside portal1 = {'start_index': 1, 'end_index': 5, 'end_point': vertices[5]} portal1r = {'start_index': 5, 'end_index': 1, 'end_point': vertices[1]} # should be outside
print("row(4) = {}".format(mtx1.row(4))) print("") try: mtx1.column(4) except ValueError as ve: print(ve) try: mtx1.row(5) except ValueError as ve: print(ve) print("\ntesting column and row methods for Vector2") vct = Vector2(16, -12) print("Vector2 row(0) = {}".format(vct.row(0))) print("Vector2 row(1) = {}".format(vct.row(1))) print("Vector2 row(2) = {}".format(vct.row(2))) print("") print("Vector2 col(0) = {}".format(vct.column(0))) try: vct.row(3) except ValueError as ve: print(ve) try: vct.column(1) except ValueError as ve:
def apply_transform(transform, vertices): return list(transform.multiply(Vector2(vert[0], vert[1])).values[:-1] \ for vert in vertices)
import os # import pudb; pu.db here = os.path.abspath(os.path.dirname(__file__)) module_root = os.path.join(here, "..") sys.path.append(module_root) from mesh2d import Vector2 # check equality and inequality operations print("\nshould be (0,4)") rel1 = Vector2.lines_intersect( Vector2(0., 0.), Vector2(0., 5.), Vector2(2., 4.), Vector2(-2., 4.),) print(rel1.intersection) print(rel1.identical) print("\nshould be (0,5)") rel1 = Vector2.lines_intersect( Vector2(0., 0.), Vector2(0., 5.), Vector2(2., 5.), Vector2(-2., 5.),) print(rel1.intersection) print(rel1.identical)