graphics programming in c

17
Graphics Programming in C Kasun Ranga Wijeweera (Email: [email protected])

Upload: kasun-ranga-wijeweera

Post on 13-Jun-2015

1.590 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: Graphics Programming in C

Graphics Programming in C

Kasun Ranga Wijeweera

(Email: [email protected])

Page 2: Graphics Programming in C

Evaluation Criteria• Project: 40%• End Semester Examination: 60%

Page 3: Graphics Programming in C

Course Content• Introduction to Graphics Programming in C• Points, Lines and Polygons• Line Drawing Algorithms • Circle Drawing Algorithms• Area Filling Algorithms• Geometric Transformations in 2D• Geometric Transformations in 3D• Line Clipping Algorithms in 2D• Line Clipping Algorithms in 3D

Page 4: Graphics Programming in C

Integrated Development Environment• Turbo C++ , Version 3.0, Copyright (c) 1990, 1992 by Borland

International, Inc

Page 5: Graphics Programming in C

Graphics Header File• Go to File New • Type initgraph in the new window and right click on it• There you will find a program and using it lets try to create a

header file to initialize and exit graphics mode• Open another file and save it as grap.h

Page 6: Graphics Programming in C

The grap.h Header File#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

void ginit()

{

……

……

}

void gexit()

{

……

……

}

Page 7: Graphics Programming in C

The ginit() Functionvoid ginit()

{

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */

initgraph(&gdriver, &gmode, "D:/Tcpp/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); /* return with error code */

}

}

Page 8: Graphics Programming in C

The gexit() Functionvoid gexit()

{

closegraph();

}

Page 9: Graphics Programming in C

The First Program• Open a new file and save it as first.c• There you have to include the grap.h header file that had been

created earlier• Then your first program should look like as follows and

running it will give a black and empty screen

#include<stdio.h>

#include<conio.h>

#include"D:/GP/header/grap.h"

void main()

{

ginit();

getch();

gexit();

}

Page 10: Graphics Programming in C

The Output Screen• Execute following code to get the maximum x and maximum

y values of the output screen

#include<stdio.h>

#include<conio.h>

#include"D:/GP/header/grap.h"

void main()

{

ginit();

printf("%d %d",getmaxx(),getmaxy());

getch();

gexit();

}

Page 11: Graphics Programming in C

The Output Screenx

y

Page 12: Graphics Programming in C

The Output Screen• The y-axis is upside down?• The solution:

(x,y) (x, getmaxy() - y)

x

y x

y

Page 13: Graphics Programming in C

Better Approximation as an Integer

FLOOR (x + 0.5)

Page 14: Graphics Programming in C

Approximating x and y Coordinatesint dpx(double x)

{

int p;

p=(int)(x+0.5);

return p;

}

int dpy(double y)

{

int p;

p=(int)(y+0.5);

p=getmaxy()-p;

return p;

}

Page 15: Graphics Programming in C

Displaying a Pointvoid main()

{

ginit();

putpixel(dpx(50.2),dpy(70.7),15);

getch();

gexit();

}

Page 16: Graphics Programming in C

Any Questions?

Page 17: Graphics Programming in C

Thank You!