def fromFoci(cls, f1, f2, f3): """Create an ellipse from foci. f1 and f2 are foci f3 is the axis_b /----f3----\ / \ | f1 f2 | \ / \----------/ """ if not isinstance(f1, (QgsPoint, CADPoint)) \ or not isinstance(f2, (QgsPoint, CADPoint)) \ or not isinstance(f3, (QgsPoint, CADPoint)): raise AttributeError f1 = CADPoint(f1) f2 = CADPoint(f2) f3 = CADPoint(f3) dist_f1f2 = f1.distance(f2) dist_f1f3 = f1.distance(f3) dist_f2f3 = f2.distance(f3) dist_tot = dist_f1f3 + dist_f2f3 angle_exist = f1.getAngleOfLineBetweenTwoPoints(f2) center_f1f2 = f1.midpoint(f2) axis_a = dist_tot / 2.0 axis_b = math.sqrt((dist_tot / 2.0)**2.0 - (dist_f1f2 / 2.0)**2.0) return cls(center_f1f2, axis_a, axis_b, angle_exist)
def by2Corners(cls, p1, p2, nbEdges=5): """CADRegularPolygon by 2 corners (AB): A----B / \ / \ \ / \------/ """ if not isinstance(p1, (QgsPoint, CADPoint)) or \ not isinstance(p2, (QgsPoint, CADPoint)) or \ not isinstance(nbEdges, (long, int)): raise AttributeError else: p1 = CADPoint(p1) p2 = CADPoint(p2) angle_exist = p1.getAngleOfLineBetweenTwoPoints(p2) pm = p1.midpoint(p2) length = p1.distance(pm) angle = (180.0 - (360.0 / nbEdges)) / 2.0 hypo = length / math.cos(math.radians(angle)) pc = p1.pointProjected(hypo, angle_exist + angle) return cls(pc, p1, nbEdges) return None
def by2Points(cls, p1, p2): """Create a CADCircle with 2 points (from diameter : AB) /------\ | | A B | | \------/ """ if not all(isinstance(p, (QgsPoint, CADPoint)) for p in [p1, p2]): raise AttributeError p1 = CADPoint(p1) p2 = CADPoint(p2) center = p1.midpoint(p2) radius = p1.distance(center) return cls(center, radius)
def byExtent(cls, p1, p2): """Create an ellipse by extent /----------\ p2 / \ | | \ / p1 \----------/ """ if not isinstance(p1, (QgsPoint, CADPoint)) \ or not isinstance(p2, (QgsPoint, CADPoint)): raise AttributeError p1 = CADPoint(p1) p2 = CADPoint(p2) pc = p1.midpoint(p2) xOffset = (abs(p2.x - p1.x)) / 2.0 yOffset = (abs(p2.y - p1.y)) / 2.0 return cls(pc, xOffset, yOffset)
def byCenterPointAngle(cls, ptCenter, ptStart, inAngle, direction=1): """Create a CADCirclurArc with by Center C a point A and angle A C | \------/Angle """ if not all([isinstance(p, (CADPoint, QgsPoint)) for p in [ptCenter, ptStart]]) or \ not isinstance(inAngle, (int, long, float)) or \ not isinstance(direction, (int, long)): raise AttributeError ptCenter = CADPoint(ptCenter) ptStart = CADPoint(ptStart) angle = ptCenter.getAngleOfLineBetweenTwoPoints(ptStart) dist = ptCenter.distance(ptStart) if inAngle < 0: inAngle += 360 ptAngle = 0 if direction == 1: ptAngle = angle - (360 - inAngle) elif direction == -1: ptAngle = angle + inAngle ptEnd = ptCenter.pointProjected(dist, ptAngle) ptAngle_arc = ptCenter.getAngleOfLineBetweenTwoPoints( ptStart.midpoint(ptEnd)) if direction == 1: if inAngle < 180: ptAngle_arc = ptAngle_arc + 180 if direction == -1: if inAngle > 180: ptAngle_arc = ptAngle_arc - 180 ptArc = ptCenter.pointProjected(dist, ptAngle_arc) return cls(ptStart, ptEnd, ptArc)