Commit 42c32602 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

move the Point/vec2 struct to core/vmath.{h,cc}

parent d8dc37da
Loading
Loading
Loading
Loading
+0 −79
Original line number Diff line number Diff line
@@ -19,85 +19,6 @@

namespace fviz {

Point::Point() :
    x(0.0f),
    y(0.0f) {}

Point::Point(
    double _x,
    double _y) :
    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};
}

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

vec2 vec2_from_deg(double deg) {
  double a = (deg - 180.0) / 180.0 * M_PI;
  return {cos(a), sin(a)};
}

vec2 vec2_mean(const vec2* v, size_t v_len) {
  vec2 m;

  for (size_t i = 0; i < v_len; ++i) {
    m = vec2_add(m, v[i]);
  }

  return vec2_mul(m, 1.0 / v_len);
}

void vec2_sort_cw(vec2* v, size_t v_len) {
  auto m = vec2_mean(v, v_len);

  std::sort(v, v + v_len, [&m] (const auto& a, const auto& b) {
    auto da = vec2_sub(a, m);
    auto db = vec2_sub(b, m);
    return atan2(db.y, db.x) < atan2(da.y, da.x);
  });
}

void vec2_sort_ccw(vec2* v, size_t v_len) {
  auto m = vec2_mean(v, v_len);

  std::sort(v, v + v_len, [&m] (const auto& a, const auto& b) {
    auto da = vec2_sub(a, m);
    auto db = vec2_sub(b, m);
    return atan2(da.y, da.x) < atan2(db.y, db.x);
  });
}

std::ostream& operator <<(std::ostream& os, const Point& p) {
  os << "Point(";
  os << p.x;
  os << ", ";
  os << p.y;
  os << ")";
  return os;
}

Rectangle::Rectangle() :
    x(0.0f),
    y(0.0f),
+2 −32
Original line number Diff line number Diff line
@@ -16,17 +16,11 @@
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include "vmath.h"

namespace fviz {

struct Point {
  Point();
  Point(double x, double y);
  double x;
  double y;
};

using vec2 = Point;
using Point = vec2;

struct Rectangle {
  Rectangle();
@@ -37,30 +31,6 @@ 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);
double vec2_dot(const vec2& a, const vec2& b);
vec2 vec2_from_deg(double deg);

/**
 * Find the mean value of a list of vectors
 */
vec2 vec2_mean(const vec2* v, size_t v_len);

/**
 * Sort an array of vec2 objects in clockwise order
 */
void vec2_sort_cw(vec2* v, size_t v_len);

/**
 * Sort an array of vec2 objects in counter-clockwise order
 */
void vec2_sort_ccw(vec2* v, size_t v_len);

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

} // namespace fviz

core/vmath.cc

0 → 100644
+102 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "fviz" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include "layout.h"

namespace fviz {

vec2::vec2() :
    x(0.0),
    y(0.0) {}

vec2::vec2(
    double _x,
    double _y) :
    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};
}

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

vec2 vec2_from_deg(double deg) {
  double a = -deg / 180.0 * M_PI;
  return {cos(a), sin(a)};
}

vec2 vec2_mean(const vec2* v, size_t v_len) {
  vec2 m;

  for (size_t i = 0; i < v_len; ++i) {
    m = vec2_add(m, v[i]);
  }

  return vec2_mul(m, 1.0 / v_len);
}

void vec2_sort_cw(vec2* v, size_t v_len) {
  auto m = vec2_mean(v, v_len);

  std::sort(v, v + v_len, [&m] (const auto& a, const auto& b) {
    auto da = vec2_sub(a, m);
    auto db = vec2_sub(b, m);
    return atan2(db.y, db.x) < atan2(da.y, da.x);
  });
}

void vec2_sort_ccw(vec2* v, size_t v_len) {
  auto m = vec2_mean(v, v_len);

  std::sort(v, v + v_len, [&m] (const auto& a, const auto& b) {
    auto da = vec2_sub(a, m);
    auto db = vec2_sub(b, m);
    return atan2(da.y, da.x) < atan2(db.y, db.x);
  });
}

std::ostream& operator <<(std::ostream& os, const vec2& p) {
  os << "vec2(";
  os << p.x;
  os << ", ";
  os << p.y;
  os << ")";
  return os;
}

} // namespace fviz

core/vmath.h

0 → 100644
+84 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "fviz" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>

namespace fviz {

struct vec2 {
  vec2();
  vec2(double x, double y);
  double x;
  double y;
};

/**
 * Return the magnitude of a vector
 */
double vec2_magnitude(const vec2& v);

/**
 * Normalize a vector
 */
vec2 vec2_normalize(const vec2& v);

/**
 * Add two vectors
 */
vec2 vec2_add(const vec2& a, const vec2& b);

/**
 * Subtract two vectors
 */
vec2 vec2_sub(const vec2& a, const vec2& b);

/**
 * Multiply a vector with a scalar
 */
vec2 vec2_mul(const vec2& v, double s);

/**
 * Compute the dot (scalar) product of two vectors
 */
double vec2_dot(const vec2& a, const vec2& b);

/**
 * Produce a vector from an angle in degrees so that zero degress maps to a
 * vector pointing "right" along the x axis (1, 0) and positive degree values
 * rotate the vector clockwise around the origin.
 */
vec2 vec2_from_deg(double deg);

/**
 * Find the mean value of a list of vectors
 */
vec2 vec2_mean(const vec2* v, size_t v_len);

/**
 * Sort an array of vec2 objects in clockwise order
 */
void vec2_sort_cw(vec2* v, size_t v_len);

/**
 * Sort an array of vec2 objects in counter-clockwise order
 */
void vec2_sort_ccw(vec2* v, size_t v_len);

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

} // namespace fviz