Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
58 views20 pages

CG ASsignment

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 20

Q1 Implement various line drawing algorithm &

compare it on the basis of performance and prepare


the table.

Bresenham's Line Algorithm


#include<stdio.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;
}

DDA Algorithm

#include <bits/stdc++.h>

using namespace std;

// function for rounding off the pixels

int round(float n)

if (n - (int)n < 0.5)

return (int)n;

return (int)(n + 1);

// Function for line generation

void DDALine(int x0, int y0, int x1, int y1)

// Calculate dx and dy

int dx = x1 - x0;

int dy = y1 - y0;

int step;

// If dx > dy we will take step as dx


// else we will take step as dy to draw the complete

// line

if (abs(dx) > abs(dy))

step = abs(dx);

else

step = abs(dy);

// Calculate x-increment and y-increment for each step

float x_incr = (float)dx / step;

float y_incr = (float)dy / step;

// Take the initial points as x and y

float x = x0;

float y = y0;

for (int i = 0; i < step; i++) {

// putpixel(round(x), round(y), WHITE);

cout << round(x) << " " << round(y) << "\n";

x += x_incr;

y += y_incr;

// delay(10);

// Driver code

int main()

int x0 = 200, y0 = 180, x1 = 180, y1 = 160;


// Function call

DDALine(x0, y0, x1, y1);

return 0;

Q2 Implement various Circle drawing algorithm &


compare it on the basis of performance and prepare the
table.
Bresenham's Circle Algorithm
#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

#include <math.h>

void EightWaySymmetricPlot(int xc,int yc,int x,int y)

putpixel(x+xc,y+yc,RED);

putpixel(x+xc,-y+yc,YELLOW);

putpixel(-x+xc,-y+yc,GREEN);

putpixel(-x+xc,y+yc,YELLOW);

putpixel(y+xc,x+yc,12);

putpixel(y+xc,-x+yc,14);

putpixel(-y+xc,-x+yc,15);

putpixel(-y+xc,x+yc,6);

void BresenhamCircle(int xc,int yc,int r)

int x=0,y=r,d=3-(2*r);
EightWaySymmetricPlot(xc,yc,x,y);

while(x<=y)

if(d<=0)

d=d+(4*x)+6;

else

d=d+(4*x)-(4*y)+10;

y=y-1;

x=x+1;

EightWaySymmetricPlot(xc,yc,x,y);

int main(void)

/* request auto detection */

int xc,yc,r,gdriver = DETECT, gmode, errorcode;

/* initialize graphics and local variables */

initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */

printf("Graphics error: %s\n", grapherrormsg(errorcode));


printf("Press any key to halt:");

getch();

exit(1); /* terminate with an error code */

printf("Enter the values of xc and yc :");

scanf("%d%d",&xc,&yc);

printf("Enter the value of radius :");

scanf("%d",&r);

BresenhamCircle(xc,yc,r);

getch();

closegraph();

return 0;

Mid point circle drawing Algorithm

#include <graphics.h>

#include <stdlib.h>

#include <math.h>

#include <stdio.h>

#include <conio.h>

#include <iostream.h>

class bresen

float x, y,a, b, r, p;

public:

void get ();

void cal ();

};

void main ()
{

bresen b;

b.get ();

b.cal ();

getch ();

Void bresen :: get ()

cout<<"ENTER CENTER AND RADIUS";

cout<< "ENTER (a, b)";

cin>>a>>b;

cout<<"ENTER r";

cin>>r;

void bresen ::cal ()

/* request auto detection */

int gdriver = DETECT,gmode, errorcode;

int midx, midy, i;

/* initialize graphics and local variables */

initgraph (&gdriver, &gmode, " ");

/* read result of initialization */

errorcode = graphresult ();

if (errorcode ! = grOK) /*an error occurred */

printf("Graphics error: %s \n", grapherrormsg (errorcode);

printf ("Press any key to halt:");

getch ();

exit (1); /* terminate with an error code */

x=0;
y=r;

putpixel (a, b+r, RED);

putpixel (a, b-r, RED);

putpixel (a-r, b, RED);

putpixel (a+r, b, RED);

p=5/4)-r;

while (x<=y)

If (p<0)

p+= (4*x)+6;

else

p+=(2*(x-y))+5;

y--;

x++;

putpixel (a+x, b+y, RED);

putpixel (a-x, b+y, RED);

putpixel (a+x, b-y, RED);

putpixel (a+x, b-y, RED);

putpixel (a+x, b+y, RED);

putpixel (a+x, b-y, RED);

putpixel (a-x, b+y, RED);

putpixel (a-x, b-y, RED);

Q3 Implement Cohen Sutherland and Cyrus Back line


clipping algorithms and display the clipped portion of
line as demonstration.
Cohen Sutherland Line Clipping
#include <iostream>

using namespace std;

// Defining region codes

const int INSIDE = 0; // 0000

const int LEFT = 1; // 0001

const int RIGHT = 2; // 0010

const int BOTTOM = 4; // 0100

const int TOP = 8; // 1000

// Defining x_max, y_max and x_min, y_min for

// clipping rectangle. Since diagonal points are

// enough to define a rectangle

const int x_max = 10;

const int y_max = 8;

const int x_min = 4;

const int y_min = 4;

// Function to compute region code for a point(x, y)

int computeCode(double x, double y)

// initialized as being inside

int code = INSIDE;

if (x < x_min) // to the left of rectangle

code |= LEFT;

else if (x > x_max) // to the right of rectangle

code |= RIGHT;

if (y < y_min) // below the rectangle

code |= BOTTOM;
else if (y > y_max) // above the rectangle

code |= TOP;

return code;

// Implementing Cohen-Sutherland algorithm

// Clipping a line from P1 = (x2, y2) to P2 = (x2, y2)

void cohenSutherlandClip(double x1, double y1,

double x2, double y2)

// Compute region codes for P1, P2

int code1 = computeCode(x1, y1);

int code2 = computeCode(x2, y2);

// Initialize line as outside the rectangular window

bool accept = false;

while (true) {

if ((code1 == 0) && (code2 == 0)) {

// If both endpoints lie within rectangle

accept = true;

break;

else if (code1 & code2) {

// If both endpoints are outside rectangle,

// in same region

break;

else {

// Some segment of line lies within the


// rectangle

int code_out;

double x, y;

// At least one endpoint is outside the

// rectangle, pick it.

if (code1 != 0)

code_out = code1;

else

code_out = code2;

// Find intersection point;

// using formulas y = y1 + slope * (x - x1),

// x = x1 + (1 / slope) * (y - y1)

if (code_out & TOP) {

// point is above the clip rectangle

x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);

y = y_max;

else if (code_out & BOTTOM) {

// point is below the rectangle

x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);

y = y_min;

else if (code_out & RIGHT) {

// point is to the right of rectangle

y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);

x = x_max;

else if (code_out & LEFT) {

// point is to the left of rectangle


y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);

x = x_min;

// Now intersection point x, y is found

// We replace point outside rectangle

// by intersection point

if (code_out == code1) {

x1 = x;

y1 = y;

code1 = computeCode(x1, y1);

else {

x2 = x;

y2 = y;

code2 = computeCode(x2, y2);

if (accept) {

cout << "Line accepted from " << x1 << ", "

<< y1 << " to " << x2 << ", " << y2 << endl;

// Here the user can add code to display the rectangle

// along with the accepted (portion of) lines

else

cout << "Line rejected" << endl;

// Driver code

int main()
{

// First Line segment

// P11 = (5, 5), P12 = (7, 7)

cohenSutherlandClip(5, 5, 7, 7);

// Second Line segment

// P21 = (7, 9), P22 = (11, 4)

cohenSutherlandClip(7, 9, 11, 4);

// Third Line segment

// P31 = (1, 5), P32 = (4, 1)

cohenSutherlandClip(1, 5, 4, 1);

return 0;

Cyrus Back line clipping algorithm


#include <SFML/Graphics.hpp>

#include <iostream>

#include <utility>

#include <vector>

using namespace std;

using namespace sf;

// Function to draw a line in SFML

void drawline(RenderWindow* window, pair<int, int> p0, pair<int, int> p1)

Vertex line[] = {

Vertex(Vector2f(p0.first, p0.second)),

Vertex(Vector2f(p1.first, p1.second))
};

window->draw(line, 2, Lines);

// Function to draw a polygon, given vertices

void drawPolygon(RenderWindow* window, pair<int, int> vertices[], int n)

for (int i = 0; i < n - 1; i++)

drawline(window, vertices[i], vertices[i + 1]);

drawline(window, vertices[0], vertices[n - 1]);

// Function to take dot product

int dot(pair<int, int> p0, pair<int, int> p1)

return p0.first * p1.first + p0.second * p1.second;

// Function to calculate the max from a vector of floats

float max(vector<float> t)

float maximum = INT_MIN;

for (int i = 0; i < t.size(); i++)

if (t[i] > maximum)

maximum = t[i];

return maximum;

// Function to calculate the min from a vector of floats

float min(vector<float> t)

{
float minimum = INT_MAX;

for (int i = 0; i < t.size(); i++)

if (t[i] < minimum)

minimum = t[i];

return minimum;

// Cyrus Beck function, returns a pair of values

// that are then displayed as a line

pair<int, int>* CyrusBeck(pair<int, int> vertices[],

pair<int, int> line[], int n)

// Temporary holder value that will be returned

pair<int, int>* newPair = new pair<int, int>[2];

// Normals initialized dynamically(can do it statically also, doesn't matter)

pair<int, int>* normal = new pair<int, int>[n];

// Calculating the normals

for (int i = 0; i < n; i++) {

normal[i].second = vertices[(i + 1) % n].first - vertices[i].first;

normal[i].first = vertices[i].second - vertices[(i + 1) % n].second;

// Calculating P1 - P0

pair<int, int> P1_P0

= make_pair(line[1].first - line[0].first,

line[1].second - line[0].second);

// Initializing all values of P0 - PEi


pair<int, int>* P0_PEi = new pair<int, int>[n];

// Calculating the values of P0 - PEi for all edges

for (int i = 0; i < n; i++) {

// Calculating PEi - P0, so that the

// denominator won't have to multiply by -1

P0_PEi[i].first

= vertices[i].first - line[0].first;

// while calculating 't'

P0_PEi[i].second = vertices[i].second - line[0].second;

int *numerator = new int[n], *denominator = new int[n];

// Calculating the numerator and denominators

// using the dot function

for (int i = 0; i < n; i++) {

numerator[i] = dot(normal[i], P0_PEi[i]);

denominator[i] = dot(normal[i], P1_P0);

// Initializing the 't' values dynamically

float* t = new float[n];

// Making two vectors called 't entering'

// and 't leaving' to group the 't's

// according to their denominators

vector<float> tE, tL;


// Calculating 't' and grouping them accordingly

for (int i = 0; i < n; i++) {

t[i] = (float)(numerator[i]) / (float)(denominator[i]);

if (denominator[i] > 0)

tE.push_back(t[i]);

else

tL.push_back(t[i]);

// Initializing the final two values of 't'

float temp[2];

// Taking the max of all 'tE' and 0, so pushing 0

tE.push_back(0.f);

temp[0] = max(tE);

// Taking the min of all 'tL' and 1, so pushing 1

tL.push_back(1.f);

temp[1] = min(tL);

// Entering 't' value cannot be

// greater than exiting 't' value,

// hence, this is the case when the line

// is completely outside

if (temp[0] > temp[1]) {

newPair[0] = make_pair(-1, -1);

newPair[1] = make_pair(-1, -1);

return newPair;

}
// Calculating the coordinates in terms of x and y

newPair[0].firs

= (float)line[0].first

+ (float)P1_P0.first * (float)temp[0];

newPair[0].second

= (float)line[0].second

+ (float)P1_P0.second * (float)temp[0];

newPair[1].first

= (float)line[0].first

+ (float)P1_P0.first * (float)temp[1];

newPair[1].second

= (float)line[0].second

+ (float)P1_P0.second * (float)temp[1];

cout << '(' << newPair[0].first << ", "

<< newPair[0].second << ") ("

<< newPair[1].first << ", "

<< newPair[1].second << ")";

return newPair;

// Driver code

int main()

// Setting up a window and loop

// and the vertices of the polygon and line

RenderWindow window(VideoMode(500, 500), "Cyrus Beck");

pair<int, int> vertices[]


= { make_pair(200, 50),

make_pair(250, 100),

make_pair(200, 150),

make_pair(100, 150),

make_pair(50, 100),

make_pair(100, 50) };

// Make sure that the vertices

// are put in a clockwise order

int n = sizeof(vertices) / sizeof(vertices[0]);

pair<int, int> line[] = { make_pair(10, 10), make_pair(450, 200) };

pair<int, int>* temp1 = CyrusBeck(vertices, line, n);

pair<int, int> temp2[2];

temp2[0] = line[0];

temp2[1] = line[1];

// To allow clipping and unclipping

// of the line by just pressing a key

bool trigger = false;

while (window.isOpen()) {

window.clear();

Event event;

if (window.pollEvent(event)) {

if (event.type == Event::Closed)

window.close();

if (event.type == Event::KeyPressed)

trigger = !trigger;

drawPolygon(&window, vertices, n);

// Using the trigger value to clip


// and unclip a line

if (trigger) {

line[0] = temp1[0];

line[1] = temp1[1];

else {

line[0] = temp2[0];

line[1] = temp2[1];

drawline(&window, line[0], line[1]);

window.display();

return 0;

You might also like