Commit 2f8930a8 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add basic 2d vector ops

parent 4a6b87ce
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -13,6 +13,8 @@
 */
#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include "layout.h"

namespace fviz {
@@ -27,6 +29,27 @@ Point::Point(
    x(_x),
    y(_y) {}

double vec2_magnitude(const vec2& v) {
  return sqrt(v.x * v.x + v.y * v.y);
}

vec2 vec2_normalize(const vec2& v) {
  auto m = vec2_magnitude(v);
  return {v.x / m, v.y / m};
}

vec2 vec2_add(const vec2& a, const vec2& b) {
  return {a.x + b.x, a.y + b.y};
}

vec2 vec2_sub(const vec2& a, const vec2& b) {
  return {a.x - b.x, a.y - b.y};
}

vec2 vec2_mul(const vec2& v, double s) {
  return {v.x * s, v.y * s};
}

std::ostream& operator <<(std::ostream& os, const Point& p) {
  os << "Point(";
  os << p.x;
+8 −0
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@ struct Point {
  double y;
};

using vec2 = Point;

struct Rectangle {
  Rectangle();
  Rectangle(double x, double y, double w, double h);
@@ -35,6 +37,12 @@ struct Rectangle {
  double h;
};

double vec2_magnitude(const vec2& v);
vec2 vec2_normalize(const vec2& v);
vec2 vec2_add(const vec2& a, const vec2& b);
vec2 vec2_sub(const vec2& a, const vec2& b);
vec2 vec2_mul(const vec2& v, double s);

std::ostream& operator <<(std::ostream& os, const Point& c);
std::ostream& operator <<(std::ostream& os, const Rectangle& c);

+16 −0
Original line number Diff line number Diff line
@@ -38,6 +38,14 @@ void Path::moveTo(double x, double y) {
  data_.emplace_back(std::move(d));
}

void Path::moveTo(const Point& p) {
  PathData d;
  d.command = PathCommand::MOVE_TO;
  d.coefficients[0] = p.x;
  d.coefficients[1] = p.y;
  data_.emplace_back(std::move(d));
}

void Path::lineTo(double x, double y) {
  PathData d;
  d.command = PathCommand::LINE_TO;
@@ -46,6 +54,14 @@ void Path::lineTo(double x, double y) {
  data_.emplace_back(std::move(d));
}

void Path::lineTo(const Point& p) {
  PathData d;
  d.command = PathCommand::LINE_TO;
  d.coefficients[0] = p.x;
  d.coefficients[1] = p.y;
  data_.emplace_back(std::move(d));
}

void Path::quadraticCurveTo(double cx, double cy, double x, double y) {
  PathData d;
  d.command = PathCommand::QUADRATIC_CURVE_TO;
+3 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
#include <stdlib.h>
#include <stdint.h>
#include <vector>
#include "geometry.h"

namespace fviz {

@@ -43,7 +44,9 @@ public:
  Path(const PathData* data, size_t size);

  void moveTo(double x, double y);
  void moveTo(const Point& p);
  void lineTo(double x, double y);
  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);