0% found this document useful (0 votes)
11 views3 pages

CGL Assignment1

The document presents an implementation of the DDA Line Drawing Algorithm using OpenGL in C++. It includes functions for drawing pixels and lines, as well as a recursive function to create patterns based on user-defined coordinates. The program prompts the user for coordinates and displays the resulting patterns in a graphical window.

Uploaded by

Pratibha Kolekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

CGL Assignment1

The document presents an implementation of the DDA Line Drawing Algorithm using OpenGL in C++. It includes functions for drawing pixels and lines, as well as a recursive function to create patterns based on user-defined coordinates. The program prompts the user for coordinates and displays the resulting patterns in a graphical window.

Uploaded by

Pratibha Kolekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment No 2:

Aim: DDA Line Drawing Algorithm


Roll No: 26
Section: SE IT

Code :
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <math.h>

#define ROUND(x) ((int)(x + 0.5))

using namespace std;

int xl, x2, zl, z2;

void draw_pixel(int x, int y) {


glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

void drawline(int Xl, int Yl, int X2, int Y2) {


float x, y, dx, dy, length;
int i;

dx = X2 - Xl;
dy = Y2 - Yl;
length = (abs(dx) >= abs(dy)) ? abs(dx) : abs(dy);

dx = dx / length;
dy = dy / length;

x = Xl;
y = Yl;
i = 1;

while (i <= length) {


draw_pixel(ROUND(x), ROUND(y));
x += dx;
y += dy;
i++;
}
}

void drawpatt(int ax, int ay, int bx, int by, int cx, int cy, int dx, int dy, int n) {
int mlx, mly, m2x, m2y, m3x, m3y, m4x, m4y;

drawline(ax, ay, bx, by);


drawline(bx, by, cx, cy);
drawline(cx, cy, dx, dy);
drawline(dx, dy, ax, ay);

// Midpoint calculations
mlx = (ax + bx) / 2;
mly = (ay + by) / 2;
m2x = (bx + cx) / 2;
m2y = (by + cy) / 2;
m3x = (cx + dx) / 2;
m3y = (cy + dy) / 2;
m4x = (dx + ax) / 2;
m4y = (dy + ay) / 2;

n--;
if (n != 0) {
drawpatt(mlx, mly, m2x, m2y, m3x, m3y, m4x, m4y, n);
}
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);

glBegin(GL_LINES);
glVertex2i(-200, 0);
glVertex2i(200, 0);
glVertex2i(0, -200);
glVertex2i(0, 200);
glEnd();

drawpatt(xl, zl, xl, z2, x2, z2, x2, zl, 5);

glFlush();
}

void Init() {
glClearColor(1, 1, 1, 0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-200.0, 200.0, -200.0, 200.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv) {


printf("Enter the value of left bottom xl: ");
scanf("%d", &xl);
printf("Enter the value of left bottom yl: ");
scanf("%d", &zl);
printf("Enter the value of right bottom x2: ");
scanf("%d", &x2);
printf("Enter the value of top right y2: ");
scanf("%d", &z2);

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Pattern Drawing");

Init();
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

Output:
Enter the
value of left
bottom xl:
10
Enter the
value of left
bottom yl:
10
Enter the
value of
right bottom
x2: 100
Enter the
value of top
right y2: 100

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy