Length Toolbox::arcRadius(const Point& p1, const Point& p2, const Angle& a) noexcept { if (a == 0) { return Length(0); } else { qreal x1 = p1.getX().toMm(); qreal y1 = p1.getY().toMm(); qreal x2 = p2.getX().toMm(); qreal y2 = p2.getY().toMm(); qreal angle = a.mappedTo180deg().toRad(); qreal d = qSqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); qreal r = d / (2 * qSin(angle / 2)); return Length::fromMm(r); } }
Point Toolbox::arcCenter(const Point& p1, const Point& p2, const Angle& a) noexcept { if (a == 0) { // there is no arc center...just return the middle of start- and endpoint return (p1 + p2) / 2; } else { // http://math.stackexchange.com/questions/27535/how-to-find-center-of-an-arc-given-start-point-end-point-radius-and-arc-direc qreal x0 = p1.getX().toMm(); qreal y0 = p1.getY().toMm(); qreal x1 = p2.getX().toMm(); qreal y1 = p2.getY().toMm(); qreal angle = a.mappedTo180deg().toRad(); qreal angleSgn = (angle >= 0) ? 1 : -1; qreal d = qSqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)); qreal r = d / (2 * qSin(angle / 2)); qreal h = qSqrt(r * r - d * d / 4); qreal u = (x1 - x0) / d; qreal v = (y1 - y0) / d; qreal a = ((x0 + x1) / 2) - h * v * angleSgn; qreal b = ((y0 + y1) / 2) + h * u * angleSgn; return Point::fromMm(a, b); } }