def update_endpoint(self, end, new_endpoint): if end == DRAG_BEGINNING: diff = vec2.sub(new_endpoint, self.p0) self.p0 = new_endpoint self.p1 = vec2.add(diff, self.p1) if end == DRAG_END: diff = vec2.sub(new_endpoint, self.p3) self.p3 = new_endpoint self.p2 = vec2.add(diff, self.p2)
def mouse_update(self, mouse_x, mouse_y): if mouse_x > self.x - 5 and mouse_x < self.x + self.width + 5 and mouse_y > self.y - 5 and mouse_y < self.y + self.height + 5: for i in range(len(self.attach_points)): point = self.attach_points[i] d = vec2.abs(vec2.sub([mouse_x, mouse_y], point)) if vec2.abs(vec2.sub([mouse_x, mouse_y], point)) <= 6: self.hover = i + 1 return self.hover = ALL else: self.hover = NONE
def add_wire_straight(pnts, net, layer, width, radius=0): rpnts = [] for idx, curr in enumerate(pnts): if idx == 0 or idx + 1 == len(pnts): # first or last rpnts.append(curr) continue prev = pnts[idx - 1] next = pnts[idx + 1] vec_a = vec2.sub(prev, curr) vec_b = vec2.sub(next, curr) len_a = vec2.length(vec_a) len_b = vec2.length(vec_b) length = min(abs(radius), len_a / 2 if idx - 1 > 0 else len_a, len_b / 2 if idx + 1 < len(pnts) - 1 else len_b) if length < 10**(-PointDigits): rpnts.append(curr) else: if radius < 0: # and abs( vec2.dot( vec_a, vec_b ) ) < len_a * len_b * 0.001: # bezier circle num_divs = 15 cef = (math.sqrt(0.5) - 0.5) / 3 * 8 bpnts = [] bpnts.append(vec2.scale(length / len_a, vec_a, curr)) bpnts.append(vec2.scale(length / len_a * cef, vec_a, curr)) bpnts.append(vec2.scale(length / len_b * cef, vec_b, curr)) bpnts.append(vec2.scale(length / len_b, vec_b, curr)) num_pnts = len(bpnts) tmp = [(0, 0) for n in range(num_pnts)] rpnts.append(bpnts[0]) for i in range(1, num_divs): t = float(i) / num_divs s = 1 - t for n in range(num_pnts): tmp[n] = bpnts[n] for L in range(num_pnts - 1, 0, -1): for n in range(L): tmp[n] = vec2.scale(s, tmp[n], vec2.scale(t, tmp[n + 1])) rpnts.append(tmp[0]) rpnts.append(bpnts[-1]) else: rpnts.append(vec2.scale(length / len_a, vec_a, curr)) rpnts.append(vec2.scale(length / len_b, vec_b, curr)) for idx, curr in enumerate(rpnts): if idx == 0: continue prev = rpnts[idx - 1] if vec2.distance(prev, curr) > 0.01: add_track(prev, curr, net, layer, width)
def intersectCircleCircle(c0P, c0R, c1P, c1R): v = vec2.sub(c1P, c0P) d = vec2.length(v) R = c0R r = c1R try: x = (d * d - r * r + R * R) / (2 * d) except ZeroDivisionError: if R < r: return 'inside', () elif r > R: return 'outside', () else: return 'coincident', () k = R * R - x * x if k < 0: if x < 0: return 'inside', () else: return 'outside', () else: y = math.sqrt(k) return 'intersect', (vec2.toangle(v), vec2.toangle((x, y)))
def intersectCircleCircle(c0P, c0R, c1P, c1R): v = vec2.sub(c1P, c0P) d = vec2.length(v) R = c0R r = c1R try: x = (d*d - r*r + R*R)/(2*d) except ZeroDivisionError: if R<r: return 'inside',() elif r>R: return 'outside',() else: return 'coincident',() k = R*R - x*x if k<0: if x<0: return 'inside',() else: return 'outside',() else: y = math.sqrt(k) return 'intersect',(vec2.toangle(v),vec2.toangle((x,y)))
def draw_line(start, end, thickness, color): endpoint_offset = vec2.norm(vec2.sub(start, end)) endpoint_offset[0], endpoint_offset[1] = -endpoint_offset[1], endpoint_offset[0] #rotate 90 degrees endpoint_offset = vec2.mul(endpoint_offset, thickness/2) pyglet.graphics.draw_indexed(4, pyglet.gl.GL_TRIANGLES, [0, 1, 2, 0, 2, 3], ('v2i', (int(start[0]+endpoint_offset[0]), int(start[1]+endpoint_offset[1]), int(end[0]+endpoint_offset[0]), int(end[1]+endpoint_offset[1]), int(end[0]-endpoint_offset[0]), int(end[1]-endpoint_offset[1]), int(start[0]-endpoint_offset[0]), int(start[1]-endpoint_offset[1]))), ('c4f', color*4) )
def drawRect(pnts, layer, R=75, width=2): if R > 0: arcs = [] for idx, a in enumerate(pnts): b = pnts[idx - 1] vec = vec2.sub(b, a) length = vec2.length(vec) delta = vec2.scale(R / length, vec) na = vec2.add(a, delta) nb = vec2.sub(b, delta) ctr = vec2.add(nb, (delta[1], -delta[0])) arcs.append((ctr, na, nb)) add_line(na, nb, layer, width) for idx, (ctr, na, nb) in enumerate(arcs): arc = add_arc2(ctr, nb, arcs[idx - 1][1], -90, layer, width) # print( "na ", pnt.mils2unit( vec2.round( na ) ) ) # print( "start", arc.GetArcStart() ) # print( "nb ", pnt.mils2unit( vec2.round( nb ) ) ) # print( "end ", arc.GetArcEnd() ) else: for idx, a in enumerate(pnts): b = pnts[idx - 1] add_line(a, b, layer, width)
def draw_textured_aa_line (start, end, thickness, tex): if start != end and thickness != 0 and tex != None: endpoint_offset = vec2.norm(vec2.sub(start, end)) endpoint_offset[0], endpoint_offset[1] = -endpoint_offset[1], endpoint_offset[0] #rotate 90 degrees endpoint_offset = vec2.mul(endpoint_offset, thickness/2) pyglet.gl.glEnable(tex.target) pyglet.gl.glBindTexture(tex.target, tex.id) pyglet.graphics.draw_indexed(4, pyglet.gl.GL_TRIANGLES, [0, 1, 2, 0, 2, 3], ('v2i', (int(start[0]+endpoint_offset[0]), int(start[1]+endpoint_offset[1]), int(end[0]+endpoint_offset[0]), int(end[1]+endpoint_offset[1]), int(end[0]-endpoint_offset[0]), int(end[1]-endpoint_offset[1]), int(start[0]-endpoint_offset[0]), int(start[1]-endpoint_offset[1]))), ('t3f', tex.tex_coords) ) pyglet.gl.glDisable(tex.target)
def raster(pt0,pt1): dx,dy= vec2.sub(pt1,pt0) adx,ady= abs(dx),abs(dy) try: m= dy/dx except ZeroDivisionError: return [] b= pt0[1]-m*pt0[0] pts= [] x= int(pt0[0]) y= int(pt0[1]) if (pt0[0]%1.0)>0.5: if pt_in_pixeldiamond(pt0): pts.append( (x,y) ) else: i_y= m*(x+0.5)+b if dy*(x+0.5) - dx*(y+1) + b*dx>=0.0: y+= 1 pts.append( (x,y) ) while x<int(pt1[0])-1: if dy*(x+1.5) - adx*(y+1) + b*adx>=0.0: x+= 1 y+= 1 else: x+= 1 pts.append( (x,y) ) if (pt1[0]%1.0)>0.5: if not pt_in_pixeldiamond(pt1): pts.append( vec2.toint(pt1) ) return pts
def on_mouse_drag(x, y, dx, dy, buttons, modifiers): global xoffset, yoffset global drag_entity if not zooming: dx /= mouse_sensitivity dy /= mouse_sensitivity xoffset -= dx yoffset -= dy check_hover() if drag_entity: # transformation if not zooming: drag_entity.move(dx, dy) elif zooming: factor = math.exp(0.01* vec2.inner ((dx, dy), (1.0, 0.0)) ) drag_entity.scale_around_center(factor, -xoffset, -yoffset) #adjust opacity if drag_entity.being_created: diff = vec2.abs(vec2.sub( (xoffset, yoffset), (start_drag_x, start_drag_y))) drag_entity.scale = 100/scale_factor #TODO: move that into creation opacity = abs(diff/drag_entity.scale) if opacity >= 1.0: drag_entity.opacity = 1.0 drag_entity.being_created = False else: drag_entity.opacity = opacity # collision detection for entity in entities: if entity != drag_entity: xdiff = abs(entity.x-drag_entity.x) ydiff = abs(entity.y-drag_entity.y) min_x_diff = (entity.scale+drag_entity.scale)/2 min_y_diff = (entity.scale+drag_entity.scale)/3 if xdiff < min_x_diff and ydiff < min_y_diff: if entity.colliding == False: entity.colliding = True drag_entity.delete_on_release += 1 else: if entity.colliding == True: entity.colliding = False drag_entity.delete_on_release -= 1 if hover_entity: def drag_over_id(id, rel_x, scale): id.temp_value = None id.temp_share_binder = None id.temp_apply = None if rel_x < 0: #apply? id.temp_apply = True else: if not (drag_entity.id.is_ancestor_of(hover_entity.id) or drag_entity.id == hover_entity.id): if id.value: drag_over_id(id.value, rel_x-scale/4, scale/2) else: id.temp_value = drag_entity.id #reset is also in check_hover if rel_x < scale/4 and id.binds_id == None: id.temp_share_binder = True else: id.temp_share_binder = False assert hover_entity.colliding == True # mouse cursor should not be able to be outside of drag_entity drag_over_id(hover_entity.id, -xoffset-hover_entity.x, scale=hover_entity.scale)
import vec2, math def intersectLineCircle((p, no), (C, r)): x = vec2.sub(p, C) b = 2. * vec2.dot(no, x) c = vec2.sqr(x) - r * r dist = b * b - 4 * c if dist < 0: return None else: d = math.sqrt(dist) t0 = (-b - d) * .5 t1 = (-b + d) * .5 return (t0, t1) # def intersectLineCircle((lineP,lineN),(circleP,circleR)): # dx,dy = vec2.sub(lineP,circleP) # nx,ny = lineN # # a = (nx*nx + ny*ny) # b = 2*(dx*nx + dy*ny) # c = (dx*dx + dy*dy) - circleR*circleR # # k = b*b - 4*a*c # if k<0: # return None # else: # d = math.sqrt(k)
# ===-- Intersect2D.py ----------------------------------------------------===## # # The KLEE Symbolic Virtual Machine # # This file is distributed under the University of Illinois Open Source # License. See LICENSE.TXT for details. # # ===----------------------------------------------------------------------===## import vec2, math def intersectLineCircle((p, no), (C, r)): x = vec2.sub(p,C) b = 2.*vec2.dot(no, x) c = vec2.sqr(x) - r*r dist = b*b - 4*c if dist<0: return None else: d = math.sqrt(dist) t0 = (-b - d)*.5 t1 = (-b + d)*.5 return (t0,t1) #def intersectLineCircle((lineP,lineN),(circleP,circleR)): # dx,dy = vec2.sub(lineP,circleP) # nx,ny = lineN # # a = (nx*nx + ny*ny) # b = 2*(dx*nx + dy*ny)
def draw_thick_cubic_bezier(points, width, color): t = 0.0 curve_points = [] while t <= 1.0: x = points[0][0] * t * t * t + 3 * points[1][0] * t * t * ( 1 - t) + 3 * points[2][0] * t * (1 - t) * ( 1 - t) + points[3][0] * (1 - t) * (1 - t) * (1 - t) y = points[0][1] * t * t * t + 3 * points[1][1] * t * t * ( 1 - t) + 3 * points[2][1] * t * (1 - t) * ( 1 - t) + points[3][1] * (1 - t) * (1 - t) * (1 - t) curve_points.append((x, y)) t += 0.01 outer_points = [] outer_tex_coords = [] inner_points = [] inner_tex_coords = [] ortho_vector = vec2.norm( vec2.sub(curve_points[1], curve_points[0]) ) #todo SOLVED: What happens when two points are in the same spot? Two points never will be in the same spot, thanks to the direction always being added onto the endpoint. ortho_vector[0], ortho_vector[1] = -1 * ortho_vector[1], ortho_vector[0] ortho_vector = vec2.mul(ortho_vector, width) inner_points.append(vec2.sub(curve_points[0], ortho_vector)) outer_points.append(vec2.add(curve_points[0], ortho_vector)) diff = vec2.sub(inner_points[-1], vec2.vecint(inner_points[-1])) projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector) inner_tex_coords.extend([0.0, 1.0 - projected_diff / width / 2, 0.0]) diff = vec2.sub(outer_points[-1], vec2.vecint(outer_points[-1])) projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector) outer_tex_coords.extend([0.0, (1 - projected_diff) / width / 2, 0.0]) r = 1.0 for i in range(1, len(curve_points) - 1): ortho_vector = vec2.norm( vec2.sub(curve_points[i + 1], curve_points[i - 1])) ortho_vector[0], ortho_vector[1] = -1 * ortho_vector[1], ortho_vector[ 0] ortho_vector = vec2.mul(ortho_vector, width) inner_points.append(vec2.sub(curve_points[i], ortho_vector)) outer_points.append(vec2.add(curve_points[i], ortho_vector)) diff = vec2.sub(inner_points[-1], vec2.vecint(inner_points[-1])) projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector) inner_tex_coords.extend([r, 1.0 - projected_diff / width / 2, 0.0]) diff = vec2.sub(outer_points[-1], vec2.vecint(outer_points[-1])) projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector) outer_tex_coords.extend([r, (1 - projected_diff) / width / 2, 0.0]) r = -r + 1 #make r alternate between 0 and 1 ortho_vector = vec2.norm(vec2.sub(curve_points[-1], curve_points[-2])) ortho_vector[0], ortho_vector[1] = -1 * ortho_vector[1], ortho_vector[0] ortho_vector = vec2.mul(ortho_vector, width) inner_points.append(vec2.sub(curve_points[-1], ortho_vector)) outer_points.append(vec2.add(curve_points[-1], ortho_vector)) diff = vec2.sub(inner_points[-1], vec2.vecint(inner_points[-1])) projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector) inner_tex_coords.extend([r, 1.0 - projected_diff / width / 2, 0.0]) diff = vec2.sub(outer_points[-1], vec2.vecint(outer_points[-1])) projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector) outer_tex_coords.extend([r, (1 - projected_diff) / width / 2, 0.0]) all_points = [] tex_coords = [] tex_coords.extend(inner_tex_coords) tex_coords.extend(outer_tex_coords) for point in inner_points: x = int(point[0]) y = int(point[1]) #diff = vec2.abs((x-point[0], y-point[1])) #if bit == True: # tex_coords.extend([0.0, 1.0-diff/a, 0.0]) #else: # tex_coords.extend([1.0, 1.0-diff/a, 0.0]) #bit ^= True all_points.extend([x, y]) for point in outer_points: x = int(point[0]) y = int(point[1]) #if bit == True: # tex_coords.extend([0.0, (1-diff)/a, 0.0]) #else: # tex_coords.extend([1.0, (1-diff)/a, 0.0]) #bit ^= True all_points.extend(point) all_points = [int(p) for p in all_points] all_points = tuple(all_points) indices = [] l = len(inner_points) for i in range(l - 1): indices.append(i) indices.append(i + 1) indices.append(i + l) indices.append(i + 1) indices.append(i + 1 + l) indices.append(i + l) glEnable(gradtex.target) glBindTexture(gradtex.target, gradtex.id) pyglet.graphics.draw_indexed( len(all_points) / 2, pyglet.gl.GL_TRIANGLES, indices, ('v2i', all_points), ('t3f', tuple(tex_coords)) #('c4f', color*(len(all_points)/2)) )
def ogl_line2d_2(pt0, pt1, set_last_line= True): dx,dy= vec2.sub(pt1,pt0) adx,ady= abs(dx),abs(dy) if adx<ady: return [(y,x) for (x,y) in ogl_line2d_2((pt0[1],pt0[0]),(pt1[1],pt1[0]))] def raster(pt0,pt1): dx,dy= vec2.sub(pt1,pt0) adx,ady= abs(dx),abs(dy) try: m= dy/dx except ZeroDivisionError: return [] b= pt0[1]-m*pt0[0] pts= [] x= int(pt0[0]) y= int(pt0[1]) if (pt0[0]%1.0)>0.5: if pt_in_pixeldiamond(pt0): pts.append( (x,y) ) else: i_y= m*(x+0.5)+b if dy*(x+0.5) - dx*(y+1) + b*dx>=0.0: y+= 1 pts.append( (x,y) ) while x<int(pt1[0])-1: if dy*(x+1.5) - adx*(y+1) + b*adx>=0.0: x+= 1 y+= 1 else: x+= 1 pts.append( (x,y) ) if (pt1[0]%1.0)>0.5: if not pt_in_pixeldiamond(pt1): pts.append( vec2.toint(pt1) ) return pts def mirror_about(axis, val): return axis + (axis-val) xaxis= int(pt0[0]) yaxis= int(pt1[1]) flipx= pt1[0]<pt0[0] flipy= pt1[1]<pt0[1] if flipx: pt0,pt1= ( (mirror_about(xaxis+0.5,pt0[0]),pt0[1]), (mirror_about(xaxis+0.5,pt1[0]),pt1[1])) if flipy: pt0,pt1= ( (pt0[0],mirror_about(yaxis+0.5,pt0[1])), (pt1[0],mirror_about(yaxis+0.5,pt1[1]))) pts= raster(pt0,pt1) if flipx: pts= [(mirror_about(xaxis,x),y) for x,y in pts] if flipy: pts= [(x,mirror_about(yaxis,y)) for x,y in pts] return pts