Graphics Programming in C
Kasun Ranga Wijeweera
(Email: krw19870829@gmail.com)
Evaluation Criteria
• Project: 40%
• End Semester Examination: 60%
Course Content
• Introduction to Graphics Programming in C
• Points, Lines and Polygons
• Line Drawing Algorithms
• Circle...
Integrated Development Environment
• Turbo C++ , Version 3.0, Copyright (c) 1990, 1992 by Borland
International, Inc
Graphics Header File
• Go to File  New
• Type initgraph in the new window and right click on it
• There you will find a p...
The grap.h Header File
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void ginit()
{
……
…...
The ginit() Function
void ginit()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize gra...
The gexit() Function
void gexit()
{
closegraph();
}
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 bee...
The Output Screen
• Execute following code to get the maximum x and maximum y
values of the output screen
#include<stdio.h...
The Output Screen
x
y
The Output Screen
• The y-axis is upside down?
• The solution:
(x,y)  (x, getmaxy() - y)
x
y
x
y
Better Approximation as an Integer
FLOOR (x + 0.5)
Approximating x and y Coordinates
int dpx(double x)
{
int p;
p=(int)(x+0.5);
return p;
}
int dpy(double y)
{
int p;
p=(int...
Displaying a Point
void main()
{
ginit();
putpixel(dpx(50.2),dpy(70.7),15);
getch();
gexit();
}
Any Questions?
Thank You!
Upcoming SlideShare
Loading in...5
×

Graphics Programming in C

1,871

Published on

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,871
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
125
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Graphics Programming in C

  1. 1. Graphics Programming in C Kasun Ranga Wijeweera (Email: krw19870829@gmail.com)
  2. 2. Evaluation Criteria • Project: 40% • End Semester Examination: 60%
  3. 3. 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
  4. 4. Integrated Development Environment • Turbo C++ , Version 3.0, Copyright (c) 1990, 1992 by Borland International, Inc
  5. 5. 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
  6. 6. The grap.h Header File #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> void ginit() { …… …… } void gexit() { …… …… }
  7. 7. The ginit() Function void 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: %sn", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* return with error code */ } }
  8. 8. The gexit() Function void gexit() { closegraph(); }
  9. 9. 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(); }
  10. 10. 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(); }
  11. 11. The Output Screen x y
  12. 12. The Output Screen • The y-axis is upside down? • The solution: (x,y)  (x, getmaxy() - y) x y x y
  13. 13. Better Approximation as an Integer FLOOR (x + 0.5)
  14. 14. Approximating x and y Coordinates int 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; }
  15. 15. Displaying a Point void main() { ginit(); putpixel(dpx(50.2),dpy(70.7),15); getch(); gexit(); }
  16. 16. Any Questions?
  17. 17. Thank You!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×