-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMid Point Line Drawing Algorithm.java
More file actions
255 lines (229 loc) · 7.24 KB
/
Mid Point Line Drawing Algorithm.java
File metadata and controls
255 lines (229 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import javax.swing.JFrame;
public class lab3 implements GLEventListener{
static GLProfile profile = GLProfile.get(GLProfile.GL2);
static GLCapabilities capabilities = new GLCapabilities(profile);
// The canvas
static GLCanvas glcanvas = new GLCanvas(capabilities);
public static void main(String[] args) {
//getting the capabilities object of GL2 profile
lab3 l= new lab3();
//creating frame
glcanvas.addGLEventListener(l);
glcanvas.setSize(600, 400);
final JFrame frame = new JFrame ("straight Line");
//adding canvas to frame
frame.getContentPane().add(glcanvas);
frame.setSize(frame.getContentPane().getPreferredSize());
frame.setVisible(true);
}
public void display(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2();
gl.glPointSize(5);
gl.glBegin (GL2.GL_POINTS);//static field
//8
midpointLine(gl, 10,10,15,10);
midpointLine(gl, 10,10,10,5);
midpointLine(gl, 10,5,10,0);
midpointLine(gl, 10,0,15,0);
midpointLine(gl, 15,0,15,5);
midpointLine(gl, 15,5,15,10);
//9
midpointLine(gl, 20,10,25,10);
midpointLine(gl, 25,10,25,5);
midpointLine(gl, 20,10,20,5);
midpointLine(gl, 20,5,25,5);
midpointLine(gl, 25,5,25,0);
gl.glEnd();
}
public void midpointLine(GL2 gl, double x1, double y1, double x2, double y2) {
double dx, dy, incrE, incrNE, d, x, y;
x = x1;
y = y1;
gl.glVertex2d(x, y);
int zone = findZone(x1, y1, x2, y2);
double[] zone0points = convertToZone0(x1, y1, zone);
x1 = zone0points[0];
y1 = zone0points[1];
zone0points = convertToZone0(x2, y2, zone);
x2 = zone0points[0];
y2 = zone0points[1];
dx = x2 - x1;
dy = y2 - y1;
d = 2 * dy - dy;
incrE = 2 * dy;
incrNE = 2 * (dy - dx);
x = x1;
y = y1;
while (x < x2) {
if (d<=0) {
d = d + incrE;
x = x + 0.001;
} else {
d = d + incrNE;
x = x + 0.001;
y = y + 0.001;
}
double[] OZP = convertToOriginalZone(x, y, zone);
double u = OZP[0];
double v = OZP[1];
gl.glVertex2d(u, v);
}
}
public int findZone(double x1, double y1, double x2, double y2) {
int zone=-1;
double dx=x2-x1;
double dy=y2-y1;
if(Math.abs(dx)>Math.abs(dy)) {
if(dx>=0&&dy>=0) {
zone=0;
}
else {
if(dx<0&&dy>=0) {
zone=3;
}
else {
if(dx<0&&dy<0) {
zone=4;
}
else {
if(dx>=0&&dy<0) {
zone=7;
}
}
}
}
}
else {
if(dx>=0&&dy>=0) {
zone=1;
}
else {
if(dx<0&&dy<0) {
zone=2;
}
else {
if(dx<0&&dy<0) {
zone=5;
}
else {
zone=6;
}
}
}
}
return zone;
}
public double[] convertToZone0(double x, double y, int zone) {
double[] result=new double[2];
if(zone==0) {
result[0]=x;
result[1]=y;
}
else {
if(zone==1) {
result[0]=y;
result[1]=x;
}
else {
if(zone==2) {
result[0]=y;
result[1]=-x;
}
else {
if(zone==3) {
result[0]=-x;
result[1]=y;
}
else {
if(zone==4) {
result[0]=-x;
result[1]=-y;
}
else {
if(zone==5) {
result[0]=-y;
result[1]=-x;
}
else{
if(zone==6) {
result[0]=-y;
result[1]=x;
}
else {
result[0]=x;
result[1]=-y;
}
}
}
}
}
}
}
return result;
}
public double[] convertToOriginalZone(double x, double y, int zone) {
double[] result=new double[2];
if(zone==0) {
result[0]=x;
result[1]=y;
}
else {
if(zone==1) {
result[0]=y;
result[1]=x;
}
else {
if(zone==2) {
result[0]=-y;
result[1]=x;
}
else {
if(zone==3) {
result[0]=-x;
result[1]=y;
}
else {
if(zone==4) {
result[0]=-x;
result[1]=-y;
}
else {
if(zone==5) {
result[0]=-y;
result[1]=-x;
}
else{
if(zone==6) {
result[0]=y;
result[1]=-x;
}
else {
result[0]=x;
result[1]=-y;
}
}
}
}
}
}
}
return result;
}
public void dispose(GLAutoDrawable arg0) {
//method body
}
public void init(GLAutoDrawable drawable) {
// method body
//4. drive the display() in a loop
}
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {
// method body
}
//end of main
}//end of classimport javax.media.opengl.GL2;