def draw_corner(cnr_type, a, cnr_data, b, layer, width, dump=False): apos, aangle = a bpos, bangle = b avec = vec2.rotate(aangle) bvec = vec2.rotate(bangle + 180) curv = None if cnr_type != Bezier and abs(vec2.dot(avec, bvec)) > 0.999: cnr_type = Line #print( avec, bvec ) if cnr_type == Line: add_line(apos, bpos, layer, width) elif cnr_type == Linear: xpos, alen, blen = vec2.find_intersection(apos, avec, bpos, bvec) if cnr_data == None: add_line(apos, xpos, layer, width) add_line(bpos, xpos, layer, width) else: delta = cnr_data[0] #print( delta, alen, xpos ) amid = vec2.scale(alen - delta, avec, apos) bmid = vec2.scale(blen - delta, bvec, bpos) add_line(apos, amid, layer, width) add_line(bpos, bmid, layer, width) add_line(amid, bmid, layer, width) elif cnr_type == Bezier: num_data = len(cnr_data) alen = cnr_data[0] blen = cnr_data[-2] ndivs = cnr_data[-1] apos2 = vec2.scale(alen, avec, apos) bpos2 = vec2.scale(blen, bvec, bpos) pnts = [apos, apos2] if num_data > 3: for pt in cnr_data[1:num_data - 2]: pnts.append(pt) pnts.append(bpos2) pnts.append(bpos) curv = calc_bezier_points(pnts, ndivs) add_lines(curv, layer, width) elif cnr_type == BezierRound: radius = cnr_data[0] # _, alen, blen = vec2.find_intersection( apos, avec, bpos, bvec ) # debug = False # if alen <= radius: # print( 'BezierRound: alen < radius, {} < {}, at {}'.format( alen, radius, apos ) ) # debug = True # if blen <= radius: # print( 'BezierRound: alen < radius, {} < {}, at {}'.format( blen, radius, bpos ) ) # debug = True # amid = vec2.scale( alen - radius, avec, apos ) # bmid = vec2.scale( blen - radius, bvec, bpos ) # add_line( apos, amid, layer, width ) # add_line( bpos, bmid, layer, width ) # angle = vec2.angle( avec, bvec ) # if angle < 0: # #angle += 360 # angle *= -1 # ndivs = int( round( angle / 4.5 ) ) # #print( 'BezierRound: angle = {}, ndivs = {}'.format( angle, ndivs ) ) # coeff = (math.sqrt( 0.5 ) - 0.5) / 3 * 8 # #print( 'coeff = {}'.format( coeff ) ) # actrl = vec2.scale( alen + (coeff - 1) * radius, avec, apos ) # bctrl = vec2.scale( blen + (coeff - 1) * radius, bvec, bpos ) # pnts = [amid, actrl, bctrl, bmid] curv = calc_bezier_round_points(apos, avec, bpos, bvec, radius) add_lines(curv, layer, width) elif cnr_type == Round: radius = cnr_data[0] # print( 'Round: radius = {}'.format( radius ) ) # print( apos, avec, bpos, bvec ) xpos, alen, blen = vec2.find_intersection(apos, avec, bpos, bvec) # print( xpos, alen, blen ) debug = False if not is_supported_round_angle(aangle): pass #print( 'Round: warning aangle = {}'.format( aangle ) ) #debug = True if not is_supported_round_angle(bangle): pass #print( 'Round: warning bangle = {}'.format( bangle ) ) #debug = True if alen < radius: print('Round: alen < radius, {} < {}'.format(alen, radius)) debug = True if blen < radius: print('Round: blen < radius, {} < {}'.format(blen, radius)) debug = True if debug: add_arc(xpos, vec2.add(xpos, (10, 0)), 360, layer, width) return b, curv angle = vec2.angle(avec, bvec) angle = math.ceil(angle * 10) / 10 tangent = math.tan(abs(angle) / 2 / 180 * math.pi) side_len = radius / tangent # print( 'angle = {}, radius = {}, side_len = {}, tangent = {}'.format( angle, radius, side_len, tangent ) ) amid = vec2.scale(-side_len, avec, xpos) bmid = vec2.scale(-side_len, bvec, xpos) add_line(apos, amid, layer, width) add_line(bpos, bmid, layer, width) aperp = (-avec[1], avec[0]) if angle >= 0: ctr = vec2.scale(-radius, aperp, amid) add_arc2(ctr, bmid, amid, 180 - angle, layer, width) else: ctr = vec2.scale(+radius, aperp, amid) add_arc2(ctr, amid, bmid, 180 + angle, layer, width) elif cnr_type == Spline: vec_scale = cnr_data[0] ndivs = int(round(vec2.distance(apos, bpos) / 2.0)) avec = vec2.rotate(aangle + 90) bvec = vec2.rotate(bangle - 90) curv = calc_spline_points(apos, avec, bpos, bvec, ndivs, vec_scale) add_lines(curv, layer, width) return b, curv
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)
def transmulvec2(m_trans,v): return tuple([vec2.dot(v, m_c) for m_c in m_trans])
# ===-- 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)