def test_TFSSvg(): fiSvg = TFSSvg(backgroundColor=0xffff0000) fiSvg.addStrokePath(polygonWithPoints(TFSPoint(4, 5), TFSPoint(4, 15), TFSPoint(14, 15), TFSPoint(14, 5)), color=0x7f00ff00, strokeWidth=2) fiSvg.renderToFile('test.svg', margin=10, height=400, maxWidth=600)
def createArc(self, startAngle, endAngle): startAngle -= self.rotation endAngle -= self.rotation # startPoint = TFSPoint(self.center.x + math.cos(startAngle) * self.hRadius, # self.center.y + math.sin(startAngle) * self.vRadius) # endPoint = TFSPoint(self.center.x + math.cos(endAngle) * self.hRadius, # self.center.y + math.sin(endAngle) * self.vRadius) # normalize radians while endAngle < startAngle: endAngle += math.pi * 2 angleDiff = endAngle - startAngle # No angle should be longer than a quarter of the circle, for precision purposes. segmentCount = max(1, int(math.ceil(angleDiff / (math.pi * 0.5)))) # The bezier control point offsets have to be scaled down to reflect that each # segment is less than a "quarter circle". segmentAngle = angleDiff / segmentCount segmentFactor = segmentAngle / (math.pi * 0.5) # print 'segmentAngle', segmentAngle # print 'segmentFactor', segmentFactor # print 'segmentCount', segmentCount, type(segmentCount) # controls = [] lastAngle = None lastPoint = None segments = [] for index in xrange(segmentCount + 1): phase = index / float(segmentCount) angle = startAngle + phase * angleDiff point = TFSPoint(self.center.x + math.cos(angle) * self.hRadius, self.center.y + math.sin(angle) * self.vRadius) if index > 0: controlPoint0 = TFSPoint( lastPoint.x + math.cos(lastAngle + math.pi * 0.5) * self.hRadius * CIRCLE_SPLINE_CONSTANT * segmentFactor, lastPoint.y + math.sin(lastAngle + math.pi * 0.5) * self.vRadius * CIRCLE_SPLINE_CONSTANT * segmentFactor) controlPoint1 = TFSPoint( point.x - math.cos(angle + math.pi * 0.5) * self.hRadius * CIRCLE_SPLINE_CONSTANT * segmentFactor, point.y - math.sin(angle + math.pi * 0.5) * self.vRadius * CIRCLE_SPLINE_CONSTANT * segmentFactor) # controls.append( (angle, controlPoint0, ) ) # controls.append( (angle, controlPoint1, ) ) points = [lastPoint, controlPoint0, controlPoint1, point] if self.rotation != 0: points = [ self.rotatePointAroundCenter(point) for point in points ] segments.append(TFSSegment(*points)) pass lastAngle = angle lastPoint = point # controls.append( (angle, point, ) ) return TFSPath(False, *segments)
def getIntersectPoint(p1, p2, p3, p4): try: denom = float((p1.x - p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x - p4.x)) x = ((p1.x * p2.y - p1.y * p2.x) * (p3.x - p4.x) - (p1.x - p2.x) * (p3.x * p4.y - p3.y * p4.x)) / denom y = ((p1.x * p2.y - p1.y * p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x * p4.y - p3.y * p4.x)) / denom except ZeroDivisionError: return return TFSPoint(x, y).roundWithDefaultPrecision()
def pointFunc(point): x, y = point.x, point.y x -= minmax.minX y -= minmax.minY x *= scaling y *= scaling x += math.floor((width - contentWidth) / 2) y += math.floor((height - contentHeight) / 2) return TFSPoint(x, y)
def renderToFile(self, filepath, margin, height, maxWidth): margin = round(margin) height = round(height) maxWidth = round(maxWidth) # print 'margin, height, maxWidth', margin, height, maxWidth minmax = None for item in self.items: if minmax is None: minmax = item.minmax() else: minmax = minmaxMerge(minmax, item.minmax()) scalingY = (height - 2 * margin) / (minmax.maxY - minmax.minY) scalingX = (maxWidth - 2 * margin) / (minmax.maxX - minmax.minX) scaling = min(scalingX, scalingY) contentWidth = math.ceil((minmax.maxX - minmax.minX) * scaling) contentHeight = math.ceil((minmax.maxY - minmax.minY) * scaling) width = contentWidth + 2 * margin # print 'scaling, contentWidth, contentHeight', scaling, contentWidth, contentHeight svg_document = svgwrite.Drawing(filename = filepath, size = ( formatSvgPixels(width), formatSvgPixels(height), ) ) if self.backgroundColor is not None: fillSvgRect(svg_document, 0, 0, width, height, self.backgroundColor) for item in self.items: item.resetRenderState() item.translate(TFSPoint(-minmax.minX, -minmax.minY)) item.scale(scaling) item.translate(TFSPoint(margin, math.floor((height - contentHeight) / 2))) item.render(svg_document) if self.borderColor is not None: drawSvgRect(svg_document, 0, 0, width, height, self.borderColor) svg_document.save()
if debugMode: print 'calculateIntersectPoint.2' return p def intersectionWithTangents(p0, tangent0, p1, tangent1): p = getIntersectPoint(p0, p0.plus(tangent0), p1, p1.plus(tangent1)) return p # Test script below... if __name__ == "__main__": # line 1 and 2 cross, 1 and 3 don't but would if extended, 2 and 3 are parallel # line 5 is horizontal, line 4 is vertical p1 = TFSPoint(1,5) p2 = TFSPoint(4,7) p3 = TFSPoint(4,5) p4 = TFSPoint(3,7) p5 = TFSPoint(4,1) p6 = TFSPoint(3,3) p7 = TFSPoint(3,1) p8 = TFSPoint(3,10) p9 = TFSPoint(0,6) p10 = TFSPoint(5,6) p11 = (472.0, 116.0)
def evaluate(self, angle): angle -= self.rotation point = TFSPoint(self.center.x + math.cos(angle) * self.hRadius, self.center.y + math.sin(angle) * self.vRadius) point = self.rotatePointAroundCenter(point) return point
def rfPointToTFSPoint(rfpoint): fiPoint = TFSPoint(rfpoint.x, rfpoint.y) if setSelected: fiPoint.selected = rfpoint.selected return fiPoint