diff --git a/Geometry processing/svg_polygon.cpp b/Geometry processing/svg_polygon.cpp index 08e9529..c990869 100644 --- a/Geometry processing/svg_polygon.cpp +++ b/Geometry processing/svg_polygon.cpp @@ -1,76 +1,78 @@ #include #include "../Raytracer/vector.cpp" +#include +#include +using namespace std; // source: https://pastebin.com/bEYVtqYy // if the Polygon class name conflicts with a class in wingdi.h on Windows, use a namespace or change the name class Polygon { public: - std::vector vertices; - vector< vector > edges; + vector vertices; }; // saves a static svg file. The polygon vertices are supposed to be in the range [0..1], and a canvas of size 1000x1000 is created - void save_svg(const std::vector &polygons, std::string filename, std::string fillcol = "none") { - FILE* f = fopen(filename.c_str(), "w+"); - fprintf(f, "\n"); - for (int i=0; i\n"); - fprintf(f, "\n", fillcol.c_str()); - fprintf(f, "\n"); +void save_svg(const vector &polygons, string filename, string fillcol = "none") { + FILE* f = fopen(filename.c_str(), "w+"); + fprintf(f, "\n"); + for (int i=0; i\n"); + fprintf(f, "\n"); - fclose(f); + fprintf(f, "\"\nfill = \"%s\" stroke = \"black\"/>\n", fillcol.c_str()); + fprintf(f, "\n"); } - + fprintf(f, "\n"); + fclose(f); +} + // Adds one frame of an animated svg file. frameid is the frame number (between 0 and nbframes-1). // polygons is a list of polygons, describing the current frame. // The polygon vertices are supposed to be in the range [0..1], and a canvas of size 1000x1000 is created - void save_svg_animated(const std::vector &polygons, std::string filename, int frameid, int nbframes) { - FILE* f; - if (frameid == 0) { - f = fopen(filename.c_str(), "w+"); - fprintf(f, "\n"); - fprintf(f, "\n"); - } else { - f = fopen(filename.c_str(), "a+"); - } +void save_svg_animated(const std::vector &polygons, std::string filename, int frameid, int nbframes) { + FILE* f; + if (frameid == 0) { + f = fopen(filename.c_str(), "w+"); + fprintf(f, "\n"); fprintf(f, "\n"); - for (int i = 0; i < polygons.size(); i++) { - fprintf(f, "\n"); - } - fprintf(f, "\n"); + for (int i = 0; i < polygons.size(); i++) { + fprintf(f, "\n"); + } + fprintf(f, "\n"); + fprintf(f, ";"); + } + fprintf(f, "none\"\n keyTimes = \""); + for (int j = 0; j < nbframes; j++) { + fprintf(f, "%2.3f", j / (double)(nbframes)); + fprintf(f, ";"); + } + fprintf(f, "1\"\n dur = \"5s\"\n"); + fprintf(f, " begin = \"0s\"\n"); + fprintf(f, " repeatCount = \"indefinite\"/>\n"); + fprintf(f, "\n"); + if (frameid == nbframes - 1) { fprintf(f, "\n"); - if (frameid == nbframes - 1) { - fprintf(f, "\n"); - fprintf(f, "\n"); - } - fclose(f); - } \ No newline at end of file + fprintf(f, "\n"); + } + fclose(f); +} \ No newline at end of file diff --git a/Geometry processing/voronoi.cpp b/Geometry processing/voronoi.cpp index 4e7ae22..17f19c1 100644 --- a/Geometry processing/voronoi.cpp +++ b/Geometry processing/voronoi.cpp @@ -1,8 +1,6 @@ -#include "../Raytracer/vector.cpp" #include "svg_polygon.cpp" -#include -Vector intersect(Vector u, Vector v, vector edge) { +Vector intersect(Vector u, Vector v, vector edge) { Vector A = edge[0]; Vector B = edge[1]; Vector M = Vector((u[0] + v[0]) / 2, (u[1] + v[1]) / 2, 0.); @@ -18,32 +16,44 @@ Vector intersect(Vector u, Vector v, vector edge) { bool inside(Vector P, Vector u, Vector v) { Vector M = Vector((u[0] + v[0]) / 2, (u[1] + v[1]) / 2, 0.); - return (dot(P - M, v - u) < 0) ? true : false; + return (dot(P - M, v - u) < 0); } Polygon sutherland_hodgman(Polygon subject, Polygon clip) { - for (int i = 0; i < clip.edges.size(); i++) { - Polygon out; - + Polygon* out; + for (int i = 0; i < clip.vertices.size() - 1; i++) { + vector clip_edge(2); + clip_edge[0] = clip.vertices[i]; + clip_edge[1] = (i == clip.vertices.size() - 1) ? clip.vertices[0] : clip.vertices[i + 1]; + + out = new Polygon(); for(int j = 0; j < subject.vertices.size(); j++) { Vector current_vertex = subject.vertices[j]; - Vector previous_vertex = subject.vertices[(j > 0) ? (j - 1) : subject.vertices[subject.vertices.size() - 1]]; - Vector intersection = intersect(previous_vertex, current_vertex, clip.edges[i]); + Vector previous_vertex = subject.vertices[(j > 0) ? (j - 1) : subject.vertices.size() - 1]; + Vector intersection = intersect(previous_vertex, current_vertex, clip_edge); - if (inside(current_vertex, clip.edges[i])) { - if(!inside(previous_vertex, clip.edges[i])) { - out.vertices.push_back(intersection); + if (inside(current_vertex, clip_edge[0], clip_edge[1])) { + if(!inside(previous_vertex, clip_edge[0], clip_edge[1])) { + out->vertices.push_back(intersection); } - out.vertices.push_back(current_vertex); + out->vertices.push_back(current_vertex); } - else if (inside(previous_vertex, clip.edges[i])) { - out.vertices.push_back(intersection); + else if (inside(previous_vertex, clip_edge[0], clip_edge[1])) { + out->vertices.push_back(intersection); } - } - subject = out; + subject = *out; } - return out; + return *out; +} + +vector voronoi( ) { + +} + +int main() { + + return 0; } \ No newline at end of file diff --git a/Raytracer/vector.cpp b/Raytracer/vector.cpp index 5a23c14..9f9b1d4 100644 --- a/Raytracer/vector.cpp +++ b/Raytracer/vector.cpp @@ -1,3 +1,5 @@ +#include + class Vector { private: double coords[3];