Commit 7b3cbfad authored by Paul Asmuth's avatar Paul Asmuth
Browse files

draw circles using cubic bezier curves

parent e108a2d3
Loading
Loading
Loading
Loading
+46 −12
Original line number Diff line number Diff line
@@ -84,17 +84,6 @@ void Path::cubicCurveTo(double c1x, double c1y, double c2x, double c2y, double x
  data_.emplace_back(std::move(d));
}

void Path::arcTo(double cx, double cy, double r, double a1, double a2) {
  PathData d;
  d.command = PathCommand::ARC_TO;
  d.coefficients[0] = cx;
  d.coefficients[1] = cy;
  d.coefficients[2] = r;
  d.coefficients[3] = a1;
  d.coefficients[4] = a2;
  data_.emplace_back(std::move(d));
}

void Path::closePath() {
  PathData d;
  d.command = PathCommand::CLOSE;
@@ -153,8 +142,11 @@ Polygon2 path_to_polygon_simple(const Path& path) {
        poly.vertices.emplace_back(cmd[0], cmd[1]);
        break;
      case PathCommand::QUADRATIC_CURVE_TO:
        poly.vertices.emplace_back(cmd[2], cmd[3]);
        break;
      case PathCommand::CUBIC_CURVE_TO:
      case PathCommand::ARC_TO:
        poly.vertices.emplace_back(cmd[4], cmd[5]);
        break;
      case PathCommand::CLOSE:
        continue;
    }
@@ -218,4 +210,46 @@ Path path_transform(const Path& path, const mat3& transform) {
  return path_out;
}

void path_add_circle(Path* path, vec2 origin, double radius) {
  const double control_point_distance = 0.552284749831;

  path->moveTo(
      origin.x,
      origin.y - radius);

  path->cubicCurveTo(
      origin.x - radius * control_point_distance,
      origin.y - radius,
      origin.x - radius,
      origin.y - radius * control_point_distance,
      origin.x - radius,
      origin.y);

  path->cubicCurveTo(
      origin.x - radius,
      origin.y + radius * control_point_distance,
      origin.x - radius * control_point_distance,
      origin.y + radius,
      origin.x,
      origin.y + radius);

  path->cubicCurveTo(
      origin.x + radius * control_point_distance,
      origin.y + radius,
      origin.x + radius,
      origin.y + radius * control_point_distance,
      origin.x + radius,
      origin.y);

  path->cubicCurveTo(
      origin.x + radius,
      origin.y - radius * control_point_distance,
      origin.x + radius * control_point_distance,
      origin.y - radius,
      origin.x,
      origin.y - radius);

  path->closePath();
}

} // namespace fviz
+5 −2
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ enum class PathCommand {
  LINE_TO,
  QUADRATIC_CURVE_TO,
  CUBIC_CURVE_TO,
  ARC_TO,
  CLOSE
};

@@ -50,7 +49,6 @@ public:
  void lineTo(const Point& p);
  void quadraticCurveTo(double cx, double cy, double x, double y);
  void cubicCurveTo(double c1x, double c1y, double c2x, double c2y, double x, double y);
  void arcTo(double cx, double cy, double r, double a1, double a2);
  void closePath();

  const PathData& operator[](size_t idx) const;
@@ -85,5 +83,10 @@ Path path_from_polygon(const Polygon2& poly);
 */
Path path_transform(const Path& p, const mat3& t);

/**
 * Add a circle to a path
 */
void path_add_circle(Path* path, vec2 origin, double radius);

} // namespace fviz
+2 −6
Original line number Diff line number Diff line
@@ -79,10 +79,8 @@ Marker marker_create_circle(double border_width) {
    style.line_width = from_unit(double(size) * border_width * 0.5);

    Path path;
    path.moveTo(pos.x + size * 0.5, pos.y);
    path.arcTo(pos.x, pos.y, size * 0.5, 0, M_PI * 2);
    path_add_circle(&path, pos, size * 0.5);
    strokePath(target, path, style);

    return OK;
  };
}
@@ -90,10 +88,8 @@ Marker marker_create_circle(double border_width) {
Marker marker_create_disk() {
  return [] (const auto& pos, const auto& size, const auto& color, auto target) {
    Path path;
    path.moveTo(pos.x + size * 0.5, pos.y);
    path.arcTo(pos.x, pos.y, size * 0.5, 0, M_PI * 2);
    path_add_circle(&path, pos, size * 0.5);
    fillPath(target, path, color);

    return OK;
  };
}
+233 −233

File changed.

Preview size limit exceeded, changes collapsed.

+167 −167

File changed.

Preview size limit exceeded, changes collapsed.

Loading