def _drawTextLabel(self, transform, text, size, vector, percent=1.0): if text is None: return if vector is None: vector = (-1, 1) angle = atan2(vector[0], -vector[1]) text_size = 0.5 * size # para_style = NSMutableParagraphStyle.alloc().init() # para_style.setAlignment_(NSCenterTextAlignment) attrs = { NSFontAttributeName: NSFont.systemFontOfSize_(text_size), NSForegroundColorAttributeName: NSColor.colorWithCalibratedRed_green_blue_alpha_( text_color[0], text_color[1], text_color[2], text_color[3] * percent, ), # NSParagraphStyleAttributeName: para_style, } myString = NSString.string().stringByAppendingString_(text) bbox = myString.sizeWithAttributes_(attrs) bw = bbox.width bh = bbox.height text_pt = NSPoint() text_pt.y = 0 if -0.5 * pi < angle <= 0.5 * pi: text_pt.x = -1.3 * size - bw / 2 * cos(angle) - bh / 2 * sin(angle) else: text_pt.x = -1.3 * size + bw / 2 * cos(angle) + bh / 2 * sin(angle) text_pt = transform.transformPoint_(text_pt) rr = NSRect(origin=(text_pt.x - bw / 2, text_pt.y - bh / 2), size=(bw, bh)) if DEBUG: NSColor.colorWithCalibratedRed_green_blue_alpha_(0, 0, 0, 0.15).set() myRect = NSBezierPath.bezierPathWithRect_(rr) myRect.setLineWidth_(0.05 * size) myRect.stroke() myString.drawInRect_withAttributes_(rr, attrs)
def solveCubicBezier(p1, p2, p3, p4): """ Solve cubic Bezier equation and 1st and 2nd derivative. """ a = NSPoint() a.x = -p1.x + 3.0 * p2.x - 3.0 * p3.x + p4.x a.y = -p1.y + 3.0 * p2.y - 3.0 * p3.y + p4.y b = NSPoint() b.x = 3.0 * p1.x - 6.0 * p2.x + 3.0 * p3.x b.y = 3.0 * p1.y - 6.0 * p2.y + 3.0 * p3.y c = NSPoint() c.x = -3.0 * p1.x + 3.0 * p2.x c.y = -3.0 * p1.y + 3.0 * p2.y d = p1 return a, b, c, d
def _drawTextLabel(self, transform, text, size, vector): if vector is None: vector = (-1, 1) angle = atan2(vector[0], -vector[1]) text_size = 0.5 * size #para_style = NSMutableParagraphStyle.alloc().init() #para_style.setAlignment_(NSCenterTextAlignment) attrs = { NSFontAttributeName: NSFont.systemFontOfSize_(text_size), NSForegroundColorAttributeName: NSColor.colorWithCalibratedRed_green_blue_alpha_( 0.4, 0.4, 0.6, 0.7 ), #NSParagraphStyleAttributeName: para_style, } myString = NSString.string().stringByAppendingString_(text) bbox = myString.sizeWithAttributes_(attrs) bw = bbox.width bh = bbox.height text_pt = NSPoint() text_pt.y = 0 if -0.5 * pi < angle <= 0.5 * pi: text_pt.x = -1.3 * size - bw / 2 * cos(angle) - bh / 2 * sin(angle) else: text_pt.x = -1.3 * size + bw / 2 * cos(angle) + bh / 2 * sin(angle) text_pt = transform.transformPoint_(text_pt) rr = NSRect( origin = (text_pt.x - bw / 2, text_pt.y - bh / 2), size = (bw, bh) ) if DEBUG: NSColor.colorWithCalibratedRed_green_blue_alpha_( 0, 0, 0, 0.15 ).set() myRect = NSBezierPath.bezierPathWithRect_(rr) myRect.setLineWidth_(0.05 * size) myRect.stroke() myString.drawInRect_withAttributes_( rr, attrs )
def solveCubicBezierCurvature(a, b, c, d, t): """ Calc curvature using cubic Bezier equation and 1st and 2nd derivative. Returns position of on-curve point p1234, and vector of 1st and 2nd derivative. """ r = NSPoint() t3 = t**3 t2 = t**2 r.x = a.x * t3 + b.x * t2 + c.x * t + d.x r.y = a.y * t3 + b.y * t2 + c.y * t + d.y r1 = NSPoint() r1.x = 3 * a.x * t2 + 2 * b.x * t + c.x r1.y = 3 * a.y * t2 + 2 * b.y * t + c.y r2 = NSPoint() r2.x = 6 * a.x * t + 2 * b.x r2.y = 6 * a.y * t + 2 * b.y return (r, r1, r2, (r1.x * r2.y - r1.y * r2.x) / (r1.x**2 + r1.y**2)**1.5)
def _drawArrow(position, kind, size, vector=(-1, 1), label_size=1): angle = atan2(vector[0], -vector[1]) print vector, "%0.2f°" % degrees(angle) x, y = position head_ratio = 0.7 w = size * 0.5 tail_width = 0.3 chin = 0.5 * (w - w * tail_width) # part under the head NSColor.colorWithCalibratedRed_green_blue_alpha_( 0.9, 0.1, 0.0, 0.85 ).set() t = NSAffineTransform.transform() t.translateXBy_yBy_(x, y) t.rotateByRadians_(angle) myPath = NSBezierPath.alloc().init() myPath.moveToPoint_( (0, 0) ) myPath.relativeLineToPoint_( (-size * head_ratio, w * 0.5) ) myPath.relativeLineToPoint_( (0, -chin) ) myPath.relativeLineToPoint_( (-size * (1 - head_ratio), 0) ) myPath.relativeLineToPoint_( (0, -w * tail_width) ) myPath.relativeLineToPoint_( (size * (1 - head_ratio), 0) ) myPath.relativeLineToPoint_( (0, -chin) ) myPath.closePath() myPath.transformUsingAffineTransform_(t) myPath.fill() drawPath(myPath) myString = NSString.string().stringByAppendingString_(kind) attrs = { NSFontAttributeName: NSFont.systemFontOfSize_(label_size), NSForegroundColorAttributeName: NSColor.colorWithCalibratedRed_green_blue_alpha_( 0.4, 0.4, 0.6, 0.7 ), } bbox = myString.sizeWithAttributes_(attrs) #print bbox p = NSPoint() bw = bbox.width bh = bbox.height #print " ", cos(angle) if -0.5 * pi < angle <= 0.5 * pi: p.x, p.y = ( - size - 20 - bh/2 * sin(angle) - bw/2 * cos(angle), # + bw/2.0 * cos(angle - pi) 0 ) else: p.x, p.y = ( - size - 20 + bh/2 * sin(angle) + bw/2 * cos(angle), # + bw/2.0 * cos(angle - pi) 0, ) p = t.transformPoint_(p) #print p #fontSize(label_size) #text(kind, (p.x - bbox.width/2, p.y - bbox.height/2)) fill(None) rect(p.x - bbox.width/2, p.y - bbox.height/2, bbox.width, bbox.height) fill(1, 0, 0) #oval(p.x -bh/2.0 , p.y -bh/2.0, bh, bh) #myString.drawAtPoint_withAttributes_(p, attrs) rr = NSRect(origin=(p.x -bh/2.0, p.y -bw/2.0), size=(bw, bh)) #print rr myString.drawInRect_withAttributes_(rr, attrs) myString.drawAtPoint_withAttributes_(p, attrs)