0% found this document useful (0 votes)
4 views13 pages

Graphics Programming in C++ Examples

The document contains multiple source code snippets for graphics programming in C/C++ using libraries like graphics.h and OpenGL. It includes functions for drawing pixels, lines, circles, ellipses, and polygons, as well as handling user input and animations. Each code segment demonstrates different graphical techniques and transformations, showcasing the capabilities of the respective graphics libraries.

Uploaded by

rajsultan.rs
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)
4 views13 pages

Graphics Programming in C++ Examples

The document contains multiple source code snippets for graphics programming in C/C++ using libraries like graphics.h and OpenGL. It includes functions for drawing pixels, lines, circles, ellipses, and polygons, as well as handling user input and animations. Each code segment demonstrates different graphical techniques and transformations, showcasing the capabilities of the respective graphics libraries.

Uploaded by

rajsultan.rs
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

Source Code

#include <graphics.h> #include <stdio.h> int main()


{
int gd = DETECT, gm, color;

initgraph(&gd, &gm, "");

putpixel(85, 35, GREEN);


putpixel(30, 40, RED);
putpixel(115, 50, YELLOW);
putpixel(135, 50, CYAN);
putpixel(45, 60, BLUE);
putpixel(20, 100, WHITE);
putpixel(200, 100, LIGHTBLUE);
putpixel(150, 100, LIGHTGREEN);
putpixel(200, 50, YELLOW);
putpixel(120, 70, RED);

getch();

closegraph();

return 0;
}

Output:
Source Code

#include <graphics.h>
#include <iostream.h>
#include <math.h>
#include <dos.h>

void main( )
{ float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

cout<<"Enter the value of x1 and y1 : ";


cin>>x1>>y1;
cout<<"Enter the value of x2 and y2: ";
cin>>x2>>y2;

dx=abs(x2-x1);
dy=abs(y2-y1);

if(dx>=dy) step=dx;
else step=dy;

dx=dx/step; dy=dy/step;

x=x1; y=y1; i=1;


while(i<=step)
{ putpixel(x,y,5);
x=x+dx; y=y+dy; i=i+1; delay(100); }
closegraph();
}

Output:
Source Code

#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;
}
Output:
Source Code

#include <stdio.h>
#include <dos.h>
#include <graphics.h>

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


{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
} void circleBres(int xc, int yc, int r)
{
int x = 0, y = r; int d = 3 - 2 * r;
drawCircle(xc, yc, x, y);
while (y >= x)
{

x++;
if (d > 0)
{ y--;
d = d + 4 * (x - y) + 10;
} else
d = d + 4 * x + 6;
drawCircle(xc, yc, x, y);
delay(50);
}
} int main()
{
int xc = 50, yc = 50, r = 30; int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // initialize graph
circleBres(xc, yc, r); // function call
return 0;
}
Output:
Source Code
#incude<iostream>
using namespace std;
void midPointCircleDraw(int x_centre, int y_centre, int r)
{int x = r, y = 0;
cout << "(" << x + x_centre << ", " << y + y_centre << ") ";
if (r > 0)
{
cout << "(" << x + x_centre << ", " << -y + y_centre << ") ";
cout << "(" << y + x_centre << ", " << x + y_centre << ") ";
cout << "(" << -y + x_centre << ", " << x + y_centre << ")\n";
}
int P = 1 - r;
while (x > y)
{
y++;
if (P <= 0)
P = P + 2*y + 1;
else
{
x--;
P = P + 2*y - 2*x + 1;
}
if (x < y)
break;
cout << "(" << x + x_centre << ", " << y + y_centre << ") ";
cout << "(" << -x + x_centre << ", " << y + y_centre << ") ";
cout << "(" << x + x_centre << ", " << -y + y_centre << ") ";
cout << "(" << -x + x_centre << ", " << -y + y_centre << ")\n";

if (x != y)
{cout << "(" << y + x_centre << ", " << x + y_centre << ") ";
cout << "(" << -y + x_centre << ", " << x + y_centre << ") ";
cout << "(" << y + x_centre << ", " << -x + y_centre << ") ";
cout << "(" << -y + x_centre << ", " << -x + y_centre << ")\n";
}
}
} int
main()
{midPointCircleDraw(0, 0, 3);
return 0;
}

Output:
Source Code:
#include <bits/stdc++.h>
using namespace std; void
midptellipse(int rx, int ry,int xc, int
yc)
{
float dx, dy, d1, d2, x, y;
x = 0;
y = ry;
d1 = (ry * ry) - (rx * rx * ry) +(0.25 * rx * rx);
dx = 2 * ry * ry *
x; dy = 2 * rx * rx *
y; while (dx < dy)
{cout << x + xc << " , " << y + yc <<
endl; cout << -x + xc << " , " << y + yc <<
endl; cout << x + xc << " , " << -y + yc <<
endl;
cout << -x + xc << " , " << -y + yc << endl;
if (d1 < 0){
x++;
dx = dx + (2 * ry * ry);
d1 = d1 + dx + (ry * ry);
}
else
{
x++;
y--;
dx = dx + (2 * ry *
ry); dy = dy - (2 * rx *
rx);
d1 = d1 + dx - dy + (ry * ry); }}

d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) + ((rx * rx) * ((y - 1) * (y - 1))) -(rx *
rx * ry * ry); while (y >= 0)
{
cout << x + xc << " , " << y + yc <<
endl; cout << -x + xc << " , " << y + yc <<
endl; cout << x + xc << " , " << -y + yc <<
endl;
cout << -x + xc << " , " << -y + yc << endl;

if (d2 > 0)
{
y--;
dy = dy - (2 * rx * rx);
d2 = d2 + (rx * rx) - dy;
}
else{
y--;
x++;
dx = dx + (2 * ry *
ry); dy = dy - (2 * rx *
rx);
d2 = d2 + dx - dy + (rx * rx);
}}} int main()
{midptellipse(200, 200, 50,
100); return 0;}

Output:
Source Code:
#include <stdlib.h>
#include <GL/glut.h>
GLfloat vertices[][2] = { { -1.0,1.0 },{ -1.0,0.857 },
{ -0.857,0.857 },{ -0.857,1.0 } };
void drawObject() {
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2fv(vertices[0]);
glVertex2fv(vertices[1]);
glVertex2fv(vertices[2]);
glVertex2fv(vertices[3]);
glEnd(); } void
display(void)
{ glClearColor(1.0, 1.0, 1.0,
1.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); glShadeModel(GL_SMOOTH);
glColor3f(0.0f, 0.0f, 0.0f);
glPushMatrix();
glLoadIdentity(); glScalef(2.0,
2.0, 0.0); drawObject();
glPopMatrix();

glFlush(); }
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1600, 800);
glutInitWindowPosition(0, 0);
glutCreateWindow("Window");
glutDisplayFunc(display);
glutMainLoop(); }
Output:
Source Code:
#include<windows.h>
#include <GL/glut.h>
#include<iostream> #include <stdlib.h> using
namespace std; void handleKeypress(unsigned char
key, int x, int y)
{ switch(key)
{ case
27:
exit(0);
} } void
initRendering()
{
glEnable(GL_DEPTH_TEST);
}

void handleResize(int w,int h)


{
glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); gluPerspective(45.0,(double)w/(double)h,1.0,200.0);
}
float _angle=0.0; float
_cameraangle=30.0; void
drawScene()
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(_cameraangle,0.0f,1.0f,0.0f); //rotate object by 30 degree with respect to y-axis
glTranslatef(0.0f, 0.0f, -10.0f);

glPushMatrix(); glTranslatef(5.0f,
-1.0f, 0.0f);
glScalef(2.0f,2.0f,2.0f);
glRotatef(_angle,1.0f,3.0f,2.0f); //rotating object continuously by 2 degree
glBegin(GL_QUADS);

glVertex3f(-0.7f,0.0f,0.0);
glVertex3f(0.7f,0.0f,0.0); glVertex3f(0.5f,2.0f,0.0);
glVertex3f(-0.5f,2.0f,0.0);

glEnd();

glPopMatrix();
glutSwapBuffers();
} void update(int
value)
{ _angle+=2.0f;
if(_angle>360.f) { _angle-
=360; } glutPostRedisplay();
glutTimerFunc(25,update,0);
}

int main(int argc,char**argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(600,600);

glutCreateWindow("rotate"); initRendering();

glutDisplayFunc(drawScene); glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);

glutTimerFunc(25,update,0);
glutMainLoop(); return 0; }
Output:
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>

float width, height, r = 0.3, change = 0;

void draw(float tx, float ty)


{
glBegin(GL_LINE_LOOP);
for(int i = 1; i <= 1200; i++)
{
float x1, y1, theta;

theta = (2 * 3.14159 * i) / 1200;


x1 = r * cosf(theta) * height / width;
y1 = r * sinf(theta);

glVertex3f(x1 , y1 ,0);
}

glEnd();
glTranslatef(tx, ty, 0);
}

void display()
{ float
p[6][2];
int j = 0;

if (change == 0)
change = 1; else if
(change == 1)
change = 0;

width = glutGet(GLUT_WINDOW_WIDTH);
height = glutGet(GLUT_WINDOW_HEIGHT) ;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);

glMatrixMode(GL_MODELVIEW);
glBegin(GL_LINE_LOOP);

for(int i = 1; i <= 1200; i++)


{
float theta, x1, y1;

theta = (2 * 3.14159 * i) / 1200;


x1 = r * cosf(theta) * height / width;
y1 = r * sinf(theta);

glVertex3f(x1, y1, 0);

if (i == 100 | i == 300 | i == 500 | i == 700 | i == 900 | i == 1100)


{ if(change
== 0){
p[j][0] = x1;
p[j][1] = y1;
j++; }
}
}
glEnd();

for(int i=0;i<6 && change == 0;i++){


draw(p[i][0],p[i][1]);
}
glutSwapBuffers();
}

void main(int argc,char *argv[])


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(700,500); glutCreateWindow("circles");
glutDisplayFunc(display); glutMainLoop();
}

Output:
Source Code:
#include <GL/glut.h>
#include <cmath>

float rect_width = 50, rect_height = 30;


float rect_x = 0, rect_y = 0; float angle
= 0; float scale_factor = 1; void
drawRectangle() {
glBegin(GL_QUADS);
glVertex2f(rect_x, rect_y); glVertex2f(rect_x +
rect_width, rect_y); glVertex2f(rect_x + rect_width,
rect_y + rect_height); glVertex2f(rect_x, rect_y +
rect_height); glEnd();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity(); glTranslatef(rect_x + rect_width / 2, rect_y
+ rect_height / 2, 0); glRotatef(angle, 0, 0, 1);
glScalef(scale_factor, scale_factor, 1);
glTranslatef(-rect_width / 2, -rect_height / 2, 0);

glColor3f(0, 0, 1); // Blue color


drawRectangle();
glutSwapBuffers();
} void update(int)
{
angle += 1; // Increment the angle for rotation scale_factor = std::abs(std::sin(angle * 3.14 /
180.0)) + 0.5; // Scale with a sinusoidal function glutPostRedisplay(); glutTimerFunc(16,
update, 0); // 60 FPS
} int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("Composite Transformation with GLUT");

glOrtho(0, 800, 0, 600, -1, 1);


glutDisplayFunc(display);
glutTimerFunc(0, update, 0);

glClearColor(1, 1, 1, 1); // White background


glutMainLoop(); return 0;
}
Output:

You might also like