22Nh15 - DHMT - Lab15 - 102220026 - 102220041
22Nh15 - DHMT - Lab15 - 102220026 - 102220041
22Nh15 - DHMT - Lab15 - 102220026 - 102220041
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,400.0,0.0,400.0);
}
void linebres()
{
int x0 = 50, y0=50, xn = 300, yn = 150, x, y;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f( 1 ,0, 0);
setPixel(x0, y0); //plot first point
glFlush();
}
KẾT QUẢ:
void myInit() {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 500, 0, 500);
}
dx = x2-x1;
dy = y2-y1;
} else {
draw_pixel(x, y);
e = 2*dx-dy;
inc1 = 2*(dx-dy);
inc2 = 2*dx;
for (i=0; i<dy; i++) {
if (e >= 0) {
x += incx;
e += inc1;
}
else
e += inc2;
y += incy;
draw_pixel(x, y);
}
}
}
void myDisplay() {
drawlinebres(x1, x2, y1, y2);
glFlush();
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Bresenham's Line Drawing");
myInit();
glutDisplayFunc(myDisplay);
glutMainLoop();
}
void myDisplay()
{
glClearColor(1.0, 1.0, 1.0, 1.0);//Clear color, white
glClear(GL_COLOR_BUFFER_BIT);//Eliminate the buffer, use the above clear color to
eliminate
glFlush();//Forced refresh
}
while (x<x2):
x = x + 1
# East is Chosen
if (d<0):
xcoordinates.append(x)
ycoordinates.append(y)
print(f"x = {x}, y = {y}")
plt.plot(xcoordinates, ycoordinates)
plt.show()
if __name__=="__main__":
x1 = int(input("Enter the starting point of x: "))
y1 = int(input("Enter the starting point of y: "))
x2 = int(input("Enter the end point of x: "))
y2 = int(input("Enter the end point of y: "))
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 200.0);
while(x<=y){
// 1st octant
setPixel(xc+x, yc+y);
setPixel(xc+y, yc+x); //find other points by symmetry
// 2nd octant
setPixel(xc-x, yc+y);
setPixel(xc-y, yc+x);
// 3rd octant
setPixel(xc-y, yc-x);
setPixel(xc-x, yc-y);
//4th octant
setPixel(xc+y, yc-x);
setPixel(xc+x, yc-y);
if (p<0) {
x = x + 1;
p = p + 4*x + 6;
}
else {
x = x + 1;
y = y - 1;
p = p + 4*x - 4*y + 10;
}
}//while
glFlush();
glutMainLoop();
return 0;
}//main
y += 1
err += 1 + 2*y
if 2*(err-x) + 1 > 0:
x -= 1
err += 1 - 2*x
return points
plt.show()
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 200.0);
}
void drawcirclemidpoint(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1 , 0, 0);
while(x<=y){
// 2nd octant
setPixel(xc-x, yc+y);
setPixel(xc-y, yc+x);
// 3rd octant
setPixel(xc-y, yc-x);
setPixel(xc-x, yc-y);
//4th octant
setPixel(xc+y, yc-x);
setPixel(xc+x, yc-y);
if (p<0) {
x = x + 1;
p = p + 2*x + 3; //p = p + 2*x + 1;
}
else {
x = x + 1;
y = y - 1;
p = p + 2*x - 2*y + 5; //p = p + 2*x - 2*y + 1;
}
}//while
glFlush();
}
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 200.0);
}
void ellipse()
{
int a = 80, b = 40, xc = 100, yc = 100;
int aSq, bSq, twoASq, twoBSq, d, dx, dy, x, y;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1 , 0, 0);
aSq = a*a;
bSq = b*b;
twoASq = 2*aSq;
twoBSq = 2*bSq;
d = bSq - b*aSq + aSq/4;
dx = 0;
dy = twoASq*b;
x = 0;
y = b;
setPixel(xc + x, yc + y);
setPixel(xc - x, yc + y);
setPixel(xc + x, yc - y);
setPixel(xc - x, yc - y);
setPixel(xc + x, yc + y);
setPixel(xc - x, yc + y);
setPixel(xc + x, yc - y);
setPixel(xc - x, yc - y);
}
setPixel(xc + x, yc + y);
setPixel(xc - x, yc + y);
setPixel(xc + x, yc - y);
setPixel(xc - x, yc - y);
}
glFlush();
}
#define INC 10
#define M_PI 3.14159265358979323846
typedef struct {
int x,y;
} Point;
//-----------------------------------------
void KhoiTao(){
glClearColor(0,0,0,0);
glLineWidth(2);
wmin.x=0;wmin.y=0;
wmax.x=200;wmax.y=120;
A.x=300;
A.y=200;
B.x=50;
B.y=-100;
goc=0;
chuot=0;
}
int Ma(Point M){
int m=0;
if(M.x<wmin.x) m |=1;
if(M.x>wmax.x) m |=2;
if(M.y<wmin.y) m |=4;
if(M.y>wmax.y) m |=8;
return m;
}
void HoanVi(Point *A,Point *B){
Point T;
T=*A; *A=*B; *B=T;
}
Point Xoay(Point A,int g)
{
float goc_rad =g * M_PI/180;
Point T;
T.x=A.x*cos(goc_rad)-A.y*sin(goc_rad);
T.y=A.x*sin(goc_rad)+A.y*cos(goc_rad);
return T;
}
void VeDuongThang(Point A,Point B){
glBegin(GL_LINES);
glVertex2i(A.x,A.y);
glVertex2i(B.x,B.y);
glEnd();
}
void VeHCN(Point wmin,Point wmax,int g)
{
Point A,B,C,D;
A=wmin;
C=wmax;
B.x=wmax.x; B.y=wmin.y;
D.x=wmin.x; D.y=wmax.y;
A=Xoay(A,g);
B=Xoay(B,g);
C=Xoay(C,g);
D=Xoay(D,g);
while(thoat==0){
if((Ma(A)|Ma(B))==0) thoat=1; // A va B nam trong vung cn
ma(A)=ma(B)=0000
else if((Ma(A) & Ma(B))!=0) // AB nam hoan toan ben ngoai
vung cn
{
thoat =1; ve=0;
}else{
if(Ma(A)==0) HoanVi(&A,&B); //A nam trong vung cn
if(A.x==B.x) // A va B cung nam doc
{
if(A.y>wmax.y) A.y=wmax.y; //
else A.y=wmin.y;
}
else
{
m=(double)(B.y-A.y)/(B.x-A.x);
if(A.x<wmin.x)
{
A.y=A.y+(wmin.x-A.x)*m;
A.x=wmin.x;
}else if(A.x>wmax.x)
{
A.y=A.y+(wmax.x-A.x)*m;
A.x=wmax.x;
}else if(A.y<wmin.y)
{
A.x=A.x+(wmin.y-A.y)/m;
A.y=wmin.y;
}else if(A.y>wmax.y)
{
A.x=A.x+(wmax.y-A.y)/m;
A.y=wmax.y;
}
}
}
}
if(ve) VeDuongThang(Xoay(A,g),Xoay(B,g));
}
void XenHinhNhiPhan(Point A,Point B,Point wmin,Point wmax,int g){
Point P,Q,M;
if((Ma(A)|Ma(B))==0) VeDuongThang(Xoay(A,g),Xoay(B,g));
if((Ma(A)&Ma(B))!=0) return ;
if((Ma(A)!=0) &&(Ma(B)==0)) HoanVi(&A,&B);
if((Ma(A)==0) &&(Ma(B)!=0))
{
P=A;Q=B;
while((abs(P.x-Q.x)+abs(P.y-Q.y))>2)
{
M.x=(P.x+Q.x)/2;
M.y=(P.y+Q.y)/2;
if(Ma(M)==0) P=M;
else Q=M;
}
VeDuongThang(Xoay(A,g),Xoay(P,g));
void Mydisplay(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glFlush();
}
}
//-----------------------------------------
int main(int argc,char* arg[]){
#include <GL/glut.h>
#include <cstdio>
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
int x1=150,y1=50, x2=50, y2=250, XL=100, XR=300, YB=100, YT=200; //(x1,y1), (x2,y2) are
the end points of the straight line segment, XL is the left boundary, XR is the right
boundary, YB is the lower boundary, and YT is the upper boundary
int x1_init = 150, y1_init = 50, x2_init = 50, y2_init = 250; //Back up the end points
of the straight line segment so as to draw the straight line segment before cutting
#include <stdio.h>
#include <math.h>
printf("Step\t(x, y)\t\t\tP\n");
printf("------------------------------\n");
printf("Step\t(x, y)\t\t\tError\n");
printf("------------------------------\n");
while (x >= y) {
printf("(%d, %d)\t\t%d\n", x + x0, y + y0, err);
if (err <= 0) {
y += 1;
err += 2 * y + 1;
}
if (err > 0) {
x -= 1;
err -= 2 * x + 1;
}
}
}
while (y >= 0) {
printf("(%d, %d)\t\t%d\n", x + xc, y + yc, d);
if (d > 0) {
y--;
d -= 2 * a2;
}
x++;
d += 2 * b2;
}
}
int main() {
printf("Bresenham Line Algorithm:\n");
bresenhamLine(1, 1, 8, 5);
printf("\nBresenham Circle Algorithm:\n");
bresenhamCircle(0, 0, 5);
printf("\nBresenham Ellipse Algorithm:\n");
bresenhamEllipse(0, 0, 8, 4);
return 0;
}
2) Viết lại các chương trình trên theo hướng tối ưu về hiệu suất thực thi chương trình.
1. Đường thẳng Bresenham:
#include <stdio.h>
#include <math.h>
printf("Step\t(x, y)\t\t\tP\n");
printf("------------------------------\n");
int main() {
printf("Bresenham Line Algorithm:\n");
bresenhamLine(1, 1, 8, 5);
return 0;
}
#include <stdio.h>
while (x >= y) {
printf("(%d, %d)\t\t%d\n", x + x0, y + y0, err);
y += 1;
err += 1 + 2 * y;
if (err <= 0) {
x -= 1;
err += 1 - 2 * x;
}
}
}
int main() {
printf("Bresenham Circle Algorithm:\n");
bresenhamCircle(0, 0, 5);
return 0;
}
#include <stdio.h>
printf("Step\t(x, y)\t\t\tD\n");
printf("------------------------------\n");
while (y >= 0) {
KhoaCNTT – Trường ĐHBK 26
printf("(%d, %d)\t\t%d\n", x + xc, y + yc, d);
if (d > 0) {
y--;
d -= 2 * a2;
}
x++;
d += 2 * b2;
}
}
int main() {
printf("Bresenham Ellipse Algorithm:\n");
bresenhamEllipse(0, 0, 8, 4);
return 0;
}
----------------------------------------------------------