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 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 _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 _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 )