Skip to content

Commit

Permalink
SutherlandHodgman edit
Browse files Browse the repository at this point in the history
  • Loading branch information
mariabrbz committed Jun 15, 2020
1 parent ca7e354 commit 618a01d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 74 deletions.
114 changes: 58 additions & 56 deletions Geometry processing/svg_polygon.cpp
Original file line number Diff line number Diff line change
@@ -1,76 +1,78 @@
#include <vector>
#include "../Raytracer/vector.cpp"
#include <cstdio>
#include <string>
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<Vector> vertices;
vector< vector<Vector> > edges;
vector<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<Polygon> &polygons, std::string filename, std::string fillcol = "none") {
FILE* f = fopen(filename.c_str(), "w+");
fprintf(f, "<svg xmlns = \"http://www.w3.org/2000/svg\" width = \"1000\" height = \"1000\">\n");
for (int i=0; i<polygons.size(); i++) {
fprintf(f, "<g>\n");
fprintf(f, "<polygon points = \"");
for (int j = 0; j < polygons[i].vertices.size(); j++) {
fprintf(f, "%3.3f, %3.3f ", (polygons[i].vertices[j][0] * 1000), (1000 - polygons[i].vertices[j][1] * 1000));
}
fprintf(f, "\"\nfill = \"%s\" stroke = \"black\"/>\n", fillcol.c_str());
fprintf(f, "</g>\n");
void save_svg(const vector<Polygon> &polygons, string filename, string fillcol = "none") {
FILE* f = fopen(filename.c_str(), "w+");
fprintf(f, "<svg xmlns = \"http://www.w3.org/2000/svg\" width = \"1000\" height = \"1000\">\n");
for (int i=0; i<polygons.size(); i++) {
fprintf(f, "<g>\n");
fprintf(f, "<polygon points = \"");
for (int j = 0; j < polygons[i].vertices.size(); j++) {
fprintf(f, "%3.3f, %3.3f ", (polygons[i].vertices[j][0] * 1000), (1000 - polygons[i].vertices[j][1] * 1000));
}
fprintf(f, "</svg>\n");
fclose(f);
fprintf(f, "\"\nfill = \"%s\" stroke = \"black\"/>\n", fillcol.c_str());
fprintf(f, "</g>\n");
}

fprintf(f, "</svg>\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<Polygon> &polygons, std::string filename, int frameid, int nbframes) {
FILE* f;
if (frameid == 0) {
f = fopen(filename.c_str(), "w+");
fprintf(f, "<svg xmlns = \"http://www.w3.org/2000/svg\" width = \"1000\" height = \"1000\">\n");
fprintf(f, "<g>\n");
} else {
f = fopen(filename.c_str(), "a+");
}
void save_svg_animated(const std::vector<Polygon> &polygons, std::string filename, int frameid, int nbframes) {
FILE* f;
if (frameid == 0) {
f = fopen(filename.c_str(), "w+");
fprintf(f, "<svg xmlns = \"http://www.w3.org/2000/svg\" width = \"1000\" height = \"1000\">\n");
fprintf(f, "<g>\n");
for (int i = 0; i < polygons.size(); i++) {
fprintf(f, "<polygon points = \"");
for (int j = 0; j < polygons[i].vertices.size(); j++) {
fprintf(f, "%3.3f, %3.3f ", (polygons[i].vertices[j][0] * 1000), (1000-polygons[i].vertices[j][1] * 1000));
}
fprintf(f, "\"\nfill = \"none\" stroke = \"black\"/>\n");
}
fprintf(f, "<animate\n");
fprintf(f, " id = \"frame%u\"\n", frameid);
fprintf(f, " attributeName = \"display\"\n");
fprintf(f, " values = \"");
for (int j = 0; j < nbframes; j++) {
if (frameid == j) {
fprintf(f, "inline");
} else {
fprintf(f, "none");
}
fprintf(f, ";");
} else {
f = fopen(filename.c_str(), "a+");
}
fprintf(f, "<g>\n");
for (int i = 0; i < polygons.size(); i++) {
fprintf(f, "<polygon points = \"");
for (int j = 0; j < polygons[i].vertices.size(); j++) {
fprintf(f, "%3.3f, %3.3f ", (polygons[i].vertices[j][0] * 1000), (1000-polygons[i].vertices[j][1] * 1000));
}
fprintf(f, "none\"\n keyTimes = \"");
for (int j = 0; j < nbframes; j++) {
fprintf(f, "%2.3f", j / (double)(nbframes));
fprintf(f, ";");
fprintf(f, "\"\nfill = \"none\" stroke = \"black\"/>\n");
}
fprintf(f, "<animate\n");
fprintf(f, " id = \"frame%u\"\n", frameid);
fprintf(f, " attributeName = \"display\"\n");
fprintf(f, " values = \"");
for (int j = 0; j < nbframes; j++) {
if (frameid == j) {
fprintf(f, "inline");
} else {
fprintf(f, "none");
}
fprintf(f, "1\"\n dur = \"5s\"\n");
fprintf(f, " begin = \"0s\"\n");
fprintf(f, " repeatCount = \"indefinite\"/>\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, "</g>\n");
if (frameid == nbframes - 1) {
fprintf(f, "</g>\n");
if (frameid == nbframes - 1) {
fprintf(f, "</g>\n");
fprintf(f, "</svg>\n");
}
fclose(f);
}
fprintf(f, "</svg>\n");
}
fclose(f);
}
46 changes: 28 additions & 18 deletions Geometry processing/voronoi.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "../Raytracer/vector.cpp"
#include "svg_polygon.cpp"
#include <vector>

Vector intersect(Vector u, Vector v, vector<int> edge) {
Vector intersect(Vector u, Vector v, vector<Vector> edge) {
Vector A = edge[0];
Vector B = edge[1];
Vector M = Vector((u[0] + v[0]) / 2, (u[1] + v[1]) / 2, 0.);
Expand All @@ -18,32 +16,44 @@ Vector intersect(Vector u, Vector v, vector<int> 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<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<Polygon> voronoi( ) {

}

int main() {

return 0;
}
2 changes: 2 additions & 0 deletions Raytracer/vector.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <cmath>

class Vector {
private:
double coords[3];
Expand Down

0 comments on commit 618a01d

Please sign in to comment.