Skip to content
Snippets Groups Projects
Commit acd8ee91 authored by Brandon Lai-Cheong's avatar Brandon Lai-Cheong
Browse files

added intersections

parent cbca2fb8
No related branches found
No related tags found
No related merge requests found
Showing with 200 additions and 17 deletions
build
\ No newline at end of file
build
.vscode/c_cpp_properties.json
.vscode/launch.json
......@@ -20,9 +20,14 @@ project(intersection)
add_executable(
tests
tests/simple_triangle_tests.cpp
tests/unit_tests/simple_triangle_tests.cpp
src/intersections/intersections.cpp
src/shapes/triangle.cpp
src/shapes/triangle_edges.cpp
)
include_directories(src)
target_link_libraries(
tests
GTest::gtest_main
......
#include <shapes/triangle.h>
#include <shapes/edge.h>
#include <shapes/triangle_edges.h>
#include <vector>
bool positiveSide(Edge e, Point p) {
float h = e.p1.y - e.p2.y;
float g = e.p1.x - e.p2.x;
return -h * (p.x - e.p1.x) + g * (p.y - e.p1.y) >= 0;
}
bool pointInTriangle(Point p, Triangle t) {
// all tests must be positive
auto edges = TriangleEdges(t);
return positiveSide(edges.e1, p) &&
positiveSide(edges.e2, p) &&
positiveSide(edges.e3, p);
}
void contourize(Triangle t1, Triangle t2, std::vector<Point> newIntersections) {
}
\ No newline at end of file
#include "intersections.h"
#include <shapes/edge.h>
#include <shapes/triangle_edges.h>
#include <shapes/point.h>
#include <optional>
std::optional<float> getB(std::optional<float> slope, Point p) {
if (!slope.has_value()) {
return {};
}
return p.y - slope.value() * p.x;
}
std::optional<float> getSlope(Edge e) {
if (e.p2.x - e.p1.x == 0) {
return {};
}
return (e.p2.y - e.p1.y) / (e.p2.x - e.p1.x) ;
}
bool withinEdge(Edge e, Point p) {
float minX = std::min(e.p1.x, e.p2.x);
float maxX = std::max(e.p1.x, e.p2.x);
float minY = std::min(e.p1.y, e.p2.y);
float maxY = std::max(e.p1.y, e.p2.y);
return minX <= p.x && p.x <= maxX && minY <= p.y && p.y <= maxY;
}
void intersection(Edge e1, Edge e2, std::vector<Point> &results) {
auto slope1 = getSlope(e1);
auto slope2 = getSlope(e2);
auto b1 = getB(slope1, e1.p1);
auto b2 = getB(slope2, e2.p1);
// ignore overlapping case
if (!slope1.has_value() && !slope2.has_value()) {
return;
}
if (!slope1.has_value()) {
results.push_back(Point{e1.p1.x, slope2.value() * e1.p1.x + b2.value()});
return;
}
if (!slope2.has_value()) {
results.push_back(Point{e2.p1.x, slope1.value() * e2.p1.x + b1.value()});
return;
}
float candX = (b2.value() - b1.value()) / (slope1.value() - slope2.value());
auto candPoint = Point{ candX, slope1.value() * candX + b1.value()};
if (withinEdge(e1, candPoint) && withinEdge(e2, candPoint)) {
results.push_back(candPoint);
}
}
void intersections(Edge e1, TriangleEdges te, std::vector<Point> &results) {
intersection(e1, te.e1, results);
intersection(e1, te.e2, results);
intersection(e1, te.e3, results);
}
std::vector<Point> intersections(Triangle t1, Triangle t2) {
TriangleEdges t1Edges = TriangleEdges(t1);
TriangleEdges t2Edges = TriangleEdges(t2);
std::vector<Point> results;
intersections(t1Edges.e1, t2Edges, results);
intersections(t1Edges.e2, t2Edges, results);
intersections(t1Edges.e3, t2Edges, results);
return results;
}
#pragma once
#include <vector>
#include "../shapes/point.h"
#include "../shapes/triangle.h"
struct TrigTrigInterResults {
std::vector<Point> results;
};
std::vector<Point> intersections(Triangle t1, Triangle t2);
\ No newline at end of file
#pragma once
#include "point.h"
struct Edge {
Point p1, p2;
};
\ No newline at end of file
#pragma once
struct Point {
float x, y;
};
\ No newline at end of file
#include "triangle.h"
bool Triangle::neighbours(Triangle &other) {
return false;
}
Triangle::Triangle(Point p1, Point p2, Point p3, int depth) : p1{p1}, p2{p2}, p3{p3}, depth{depth} {}
\ No newline at end of file
#pragma once
#include "point.h"
struct Point {
float x, y;
};
struct Edge {
Point p1, p2;
};
// points specified clockwise
struct Triangle {
Point p1, p2, p3;
int depth;
// neighbours
bool neighbours(Triangle &other);
Triangle(Point p1, Point p2, Point p3, int depth);
};
#include "triangle_edges.h"
#include "triangle.h"
TriangleEdges::TriangleEdges(const Triangle &t) : e1{Edge{t.p1, t.p2}}, e2{Edge{t.p2, t.p3}}, e3{Edge{t.p3, t.p1}}{
}
\ No newline at end of file
#include "edge.h"
struct Triangle;
struct TriangleEdges {
Edge e1, e2, e3;
TriangleEdges(const Triangle &t);
};
template<class T>
class StaticVector{
int maxSize;
};
\ No newline at end of file
#pragma once
#include "triangle.h"
#include "shapes/triangle.h"
#include <vector>
#include <optional>
#include <intersections/intersections.h>
std::optional<Point> intersection(Edge e1, Edge e2) {
std::vector<Point> contourize(std::vector<Point> points) {
}
std::vector<Point> intersections(Triangle t1, Triangle t2) {
}
std::vector<Triangle> triangulate() {
}
std::vector<Triangle> unionize(Triangle t1, Triangle t2) {
// if neighbours, do nothing
if (t1.neighbours(t2)) {
return std::vector{t1, t2};
}
// at most 3? if infinite -> consider no intersections
std::vector<Point> newIntersections = intersections(t1, t2);
if (newIntersections.empty()) {
// either completely envelops each other, no intersections
return std::vector{t1, t2};
}
return triangulate();
std::vector<Point> contour = contourize();
return triangulate(contour);
}
void unionize(Triangle soup) {
}
\ No newline at end of file
#include "gtest/gtest.h"
TEST (TriangleIntersectionTests, TriangleIntersection) {
}
\ No newline at end of file
#include "gtest/gtest.h"
#include "../../src/shapes/triangle.h"
#include "../../src/intersections/intersections.h"
TEST (TriangleIntersectionTests, TriangleIntersection) {
auto t1 = Triangle(Point(0,0), Point(2,3), Point(5,0), 0);
auto t2 = Triangle(Point(3,1), Point(4,3), Point(6,1), 0);
std::vector<Point> results = intersections(t1, t2);
EXPECT_TRUE(results.size() == 2) << "Actual size: " << results.size();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment