Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clipping when any vertice lie on clipping window #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 66 additions & 58 deletions src/Sutherland-Hodgman.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <graphics.h>

int points[80][2] = { { 0 } }, new[80][2] = { { 0 } };
int points[80][2] = { { 0 } }, nw[80][2] = { { 0 } };

int leftClip(int, int);
int topClip(int, int);
Expand Down Expand Up @@ -43,6 +43,7 @@ int main() {
line(points[i][0], points[i][1], points[(i + 1) % result][0],
points[(i + 1) % result][1]);

getch();
return 0;
}

Expand All @@ -56,31 +57,32 @@ int leftClip(int limit, int xmin) {
y2 = points[(i + 1) % limit][1];
if (x2 - x1)
m = (y2 - y1) * 1.0 / (x2 - x1);

else
m = 0;
if (x1 < xmin && x2 < xmin)
continue;
if (x1 > xmin && x2 > xmin) {
new[j][0] = x2;
new[j++][1] = y2;
if (x1 >= xmin && x2 >= xmin) {
nw[j][0] = x2;
nw[j++][1] = y2;
continue;
}
if (x1 > xmin && x2 < xmin) {
new[j][0] = xmin;
new[j++][1] = y1 + m * (xmin - x1);
if (x1 >= xmin && x2 < xmin) {
nw[j][0] = xmin;
nw[j++][1] = y1 + m * (xmin - x1);
continue;
}
if (x1 < xmin && x2 > xmin) {
new[j][0] = xmin;
new[j++][1] = y1 + m * (xmin - x1);
new[j][0] = x2;
new[j++][1] = y2;
if (x1 < xmin && x2 >= xmin) {
nw[j][0] = xmin;
nw[j++][1] = y1 + m * (xmin - x1);
nw[j][0] = x2;
nw[j++][1] = y2;
}
}

for (i = 0; i < j; i++) {
points[i][0] = new[i][0];
points[i][1] = new[i][1];
new[i][0] = new[i][1] = 0;
points[i][0] = nw[i][0];
points[i][1] = nw[i][1];
nw[i][0] = nw[i][1] = 0;
}

if (j < limit)
Expand All @@ -100,31 +102,33 @@ int topClip(int limit, int ymin) {
y2 = points[(i + 1) % limit][1];
if (x2 - x1)
m = (y2 - y1) * 1.0 / (x2 - x1);
else
m = 0;

if (y1 < ymin && y2 < ymin)
continue;
if (y1 > ymin && y2 > ymin) {
new[j][0] = x2;
new[j++][1] = y2;
if (y1 >= ymin && y2 >= ymin) {
nw[j][0] = x2;
nw[j++][1] = y2;
continue;
}
if (y1 > ymin && y2 < ymin) {
new[j][0] = x1 + (ymin - y1) / m;
new[j++][1] = ymin;
if (y1 >= ymin && y2 < ymin) {
nw[j][0] = x1 + (ymin - y1) / m;
nw[j++][1] = ymin;
continue;
}
if (y1 < ymin && y2 > ymin) {
new[j][0] = x1 + (ymin - y1) / m;
new[j++][1] = ymin;
new[j][0] = x2;
new[j++][1] = y2;
if (y1 < ymin && y2 >= ymin) {
nw[j][0] = x1 + (ymin - y1) / m;
nw[j++][1] = ymin;
nw[j][0] = x2;
nw[j++][1] = y2;
}
}

for (i = 0; i < j; i++) {
points[i][0] = new[i][0];
points[i][1] = new[i][1];
new[i][0] = new[i][1] = 0;
points[i][0] = nw[i][0];
points[i][1] = nw[i][1];
nw[i][0] = nw[i][1] = 0;
}

if (j < limit)
Expand All @@ -144,31 +148,33 @@ int rightClip(int limit, int xmax) {
y2 = points[(i + 1) % limit][1];
if (x2 - x1)
m = (y2 - y1) * 1.0 / (x2 - x1);
else
m = 0;

if (x1 > xmax && x2 > xmax)
continue;
if (x1 < xmax && x2 < xmax) {
new[j][0] = x2;
new[j++][1] = y2;
if (x1 <= xmax && x2 <= xmax) {
nw[j][0] = x2;
nw[j++][1] = y2;
continue;
}
if (x1 < xmax && x2 > xmax) {
new[j][0] = xmax;
new[j++][1] = y1 + m * (xmax - x1);
if (x1 <= xmax && x2 > xmax) {
nw[j][0] = xmax;
nw[j++][1] = y1 + m * (xmax - x1);
continue;
}
if (x1 > xmax && x2 < xmax) {
new[j][0] = xmax;
new[j++][1] = y1 + m * (xmax - x1);
new[j][0] = x2;
new[j++][1] = y2;
if (x1 > xmax && x2 <= xmax) {
nw[j][0] = xmax;
nw[j++][1] = y1 + m * (xmax - x1);
nw[j][0] = x2;
nw[j++][1] = y2;
}
}

for (i = 0; i < j; i++) {
points[i][0] = new[i][0];
points[i][1] = new[i][1];
new[i][0] = new[i][1] = 0;
points[i][0] = nw[i][0];
points[i][1] = nw[i][1];
nw[i][0] = nw[i][1] = 0;
}

if (j < limit)
Expand All @@ -188,31 +194,33 @@ int bottomClip(int limit, int ymax) {
y2 = points[(i + 1) % limit][1];
if (x2 - x1)
m = (y2 - y1) * 1.0 / (x2 - x1);
else
m = 0;

if (y1 > ymax && y2 > ymax)
continue;
if (y1 < ymax && y2 < ymax) {
new[j][0] = x2;
new[j++][1] = y2;
if (y1 <= ymax && y2 <= ymax) {
nw[j][0] = x2;
nw[j++][1] = y2;
continue;
}
if (y1 < ymax && y2 > ymax) {
new[j][0] = x1 + (ymax - y1) / m;
new[j++][1] = ymax;
if (y1 <= ymax && y2 > ymax) {
nw[j][0] = x1 + (ymax - y1) / m;
nw[j++][1] = ymax;
continue;
}
if (y1 > ymax && y2 < ymax) {
new[j][0] = x1 + (ymax - y1) / m;
new[j++][1] = ymax;
new[j][0] = x2;
new[j++][1] = y2;
if (y1 > ymax && y2 <= ymax) {
nw[j][0] = x1 + (ymax - y1) / m;
nw[j++][1] = ymax;
nw[j][0] = x2;
nw[j++][1] = y2;
}
}

for (i = 0; i < j; i++) {
points[i][0] = new[i][0];
points[i][1] = new[i][1];
new[i][0] = new[i][1] = 0;
points[i][0] = nw[i][0];
points[i][1] = nw[i][1];
nw[i][0] = nw[i][1] = 0;
}

if (j < limit)
Expand Down