Skip to content

Commit 23f874b

Browse files
Final commit for today
1 parent aee2daa commit 23f874b

File tree

11 files changed

+152094
-0
lines changed

11 files changed

+152094
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
int line_num = 5;
2+
int inside=0, left=1, right=2, bottom=4, top=8;
3+
int x_max=450, y_max=400, x_min=150, y_min=200;
4+
5+
int findCode(double x, double y) {
6+
int code = inside;
7+
8+
if (x < x_min){
9+
code = code|left;}
10+
else if (x > x_max){
11+
code = code|right;}
12+
13+
if (y < y_min){
14+
code = code|top;}
15+
else if (y > y_max){
16+
code = code|bottom;}
17+
18+
return code;
19+
}
20+
21+
void line_clipping(double x1, double y1, double x2, double y2) {
22+
23+
24+
int code1 = findCode(x1, y1);
25+
int code2 = findCode(x2, y2);
26+
27+
print(x1 + " " + y1 + " ");
28+
println(code1);
29+
print(x2 + " " + y2 + " ");
30+
println(code1);
31+
32+
boolean isLine = false;
33+
34+
while (true) {
35+
if ((code1==0) && (code2==0)) {
36+
isLine=true;
37+
break;
38+
} else if ((code1 & code2)!=0) {
39+
break;
40+
} else {
41+
int result;
42+
double x=0, y=0;
43+
44+
if (code1!=0)
45+
result = code1;
46+
else
47+
result = code2;
48+
49+
if ((result & bottom)!=0) {
50+
x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
51+
y = y_max;
52+
} else if ((result & top)!=0)
53+
{
54+
x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
55+
y = y_min;
56+
} else if ((result & right)!=0)
57+
{
58+
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
59+
x = x_max;
60+
} else if ((result & left)!=0)
61+
{
62+
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
63+
x = x_min;
64+
}
65+
66+
if (result==code1) {
67+
x1 = x;
68+
y1 = y;
69+
code1 = findCode(x1, y1);
70+
} else {
71+
x2 = x;
72+
y2 = y;
73+
code2 = findCode(x2, y2);
74+
}
75+
}
76+
}
77+
if(isLine==true){
78+
stroke(255, 0, 0);
79+
strokeWeight(2);
80+
stroke(255, 0, 0);
81+
line((float)x1, (float)y1, (float)x2, (float)y2);
82+
}
83+
}
84+
85+
void setup() {
86+
87+
size(600, 600);
88+
}
89+
90+
void draw(){
91+
92+
frameRate(1);
93+
background(51);
94+
strokeWeight(2);
95+
stroke(255);
96+
rectMode(CENTER);
97+
noFill();
98+
rect(width/2, height/2, 300, 200);
99+
100+
strokeWeight(0.3);
101+
102+
for (int i=0; i<line_num; i++) {
103+
int x1 = int(random(0, width));
104+
int y1 = int(random(0, width));
105+
int x2 = int(random(0, height));
106+
int y2 = int(random(0, height));
107+
108+
strokeWeight(0);
109+
stroke(255);
110+
line(x1, y1, x2, y2);
111+
line_clipping(x1, y1, x2, y2);
112+
}
113+
114+
115+
116+
}
117+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
int max_size = 200;
2+
3+
int xIntersect(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
4+
{
5+
int num = (x1*y2 - y1*x2) * (x3-x4) - (x1-x2) * (x3*y4 - y3*x4);
6+
int den = (x1-x2) * (y3-y4) - (y1-y2) * (x3-x4);
7+
return num/den;
8+
}
9+
int yIntersect(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
10+
{
11+
int num = (x1*y2 - y1*x2) * (y3-y4) - (y1-y2) * (x3*y4 - y3*x4);
12+
int den = (x1-x2) * (y3-y4) - (y1-y2) * (x3-x4);
13+
return num/den;
14+
}
15+
16+
void edgeClip(int poly[][], int poly_size, int x1, int y1, int x2, int y2) {
17+
18+
int[][] new_poly = new int[max_size][2];
19+
int new_poly_size = 0;
20+
21+
for (int i=0; i<poly_size; i++) {
22+
int k = (i+1) % poly_size;
23+
int ix = poly[i][0], iy = poly[i][1];
24+
int kx = poly[k][0], ky = poly[k][1];
25+
26+
int i_pos = (x2-x1) * (iy-y1) - (y2-y1) * (ix-x1);
27+
int k_pos = (x2-x1) * (ky-y1) - (y2-y1) * (kx-x1);
28+
29+
if (i_pos < 0 && k_pos < 0)
30+
{
31+
new_poly[new_poly_size][0] = kx;
32+
new_poly[new_poly_size][1] = ky;
33+
++new_poly_size;
34+
} else if (i_pos >= 0 && k_pos < 0)
35+
{
36+
new_poly[new_poly_size][0] = xIntersect(x1, y1, x2, y2, ix, iy, kx, ky);
37+
new_poly[new_poly_size][1] = yIntersect(x1, y1, x2, y2, ix, iy, kx, ky);
38+
++new_poly_size;
39+
40+
new_poly[new_poly_size][0] = kx;
41+
new_poly[new_poly_size][1] = ky;
42+
++new_poly_size;
43+
} else if (i_pos < 0 && k_pos >= 0)
44+
{
45+
new_poly[new_poly_size][0] = xIntersect(x1, y1, x2, y2, ix, iy, kx, ky);
46+
new_poly[new_poly_size][1] = yIntersect(x1, y1, x2, y2, ix, iy, kx, ky);
47+
++new_poly_size;
48+
}
49+
}
50+
poly_size = new_poly_size;
51+
for (int i = 0; i < poly_size; i++)
52+
{
53+
poly[i][0] = new_poly[i][0];
54+
poly[i][1] = new_poly[i][1];
55+
}
56+
}
57+
58+
void polygonClipping(int poly[][], int poly_size, int clip[][], int clip_size) {
59+
for (int i = 0; i < clip_size; i++)
60+
{
61+
int k = (i+1) % clip_size;
62+
63+
edgeClip(poly, poly_size, clip[i][0], clip[i][1], clip[k][0], clip[k][1]);
64+
}
65+
66+
noFill();
67+
beginShape();
68+
for(int i=0; i<poly_size; i++){
69+
vertex(poly[i][0], poly[i][1]);
70+
}
71+
endShape(CLOSE);
72+
}
73+
74+
void setup() {
75+
size(600, 600);
76+
77+
int poly_size = 3;
78+
int poly_points[][] = {{100, 150}, {200, 250},{300, 200}};
79+
triangle(100, 150, 200, 250,300, 200);
80+
81+
82+
int clipper_size = 4;
83+
int clipper_points[][] = {{150, 150}, {150, 200}, {200, 200}, {200, 150} };
84+
//beginShape();
85+
//vertex(150, 150);
86+
//vertex(150, 200);
87+
//vertex(200, 200);
88+
//vertex(200, 150);
89+
//vertex(150, 150);
90+
//endShape();
91+
92+
polygonClipping(poly_points, poly_size, clipper_points, clipper_size);
93+
}

Diff for: GraphicsLab_Modelling/p5/Modelling/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Computer Graphics</title>
7+
<style> body {padding: 0; margin: 0;} </style>
8+
<script src="../p5.min.js"></script>
9+
<script src="../addons/p5.dom.min.js"></script>
10+
<script src="../addons/p5.sound.min.js"></script>
11+
<script src="sketch.js"></script>
12+
</head>
13+
<body>
14+
</body>
15+
</html>

Diff for: GraphicsLab_Modelling/p5/Modelling/sketch.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
function setup() {
2+
3+
createCanvas(1000, 735);
4+
}
5+
6+
function draw() {
7+
8+
background(55);
9+
stroke(255);
10+
strokeWeight(3);
11+
noFill();
12+
13+
14+
//body
15+
line(215,350,763,352);
16+
line(215,350,215,439);
17+
18+
arc(763,409,170,114,PI+HALF_PI,TWO_PI);
19+
line(847,405,850,439);
20+
21+
line(860,528,109,528);
22+
line(373,488,652,488);
23+
24+
//bumpers
25+
//front bumper
26+
line(781,438,850,439);
27+
line(850,438,873,444);
28+
line(774,488,850,488);
29+
line(850,488,873,444);
30+
//rear bumper
31+
line(246,442,196,439);
32+
line(247,485,208,486);
33+
line(208,486,196,439);
34+
35+
//lights
36+
//rear
37+
rect(215,375,20,15);
38+
//front
39+
ellipse(806,377,5,22);
40+
41+
//upper part
42+
line(304,350,379,240);
43+
line(379,240,592,230);
44+
line(592,230,664,350);
45+
46+
//window
47+
line(454,238,457,350);
48+
49+
//doors
50+
line(460,488,457,350);
51+
line(661,412,664,350);
52+
quad(474,357, 506,360, 505,369, 477,368);
53+
54+
//tires
55+
//left tire
56+
ellipse(314,459,135,135);
57+
ellipse(314,459,60,60);
58+
ellipse(314,459,10,10);
59+
//tire rims
60+
line(314,429,314,453);
61+
line(307,461,287,445);
62+
line(309,465,293,481);
63+
line(336,479,320,463);
64+
line(320,456,340,445);
65+
66+
67+
//right tire
68+
ellipse(713,459,135,135);
69+
ellipse(713,459,60,60);
70+
ellipse(713,459,10,10);
71+
//tire rims
72+
line(314+400,429,314+400,453);
73+
line(307+400,461,287+400,445);
74+
line(309+400,465,293+400,481);
75+
line(336+400,479,320+400,463);
76+
line(320+400,456,340+400,445);
77+
78+
}

0 commit comments

Comments
 (0)