Yeni versionları burdan yayınliicam...
/**
* Icerikler
*/
#include <conio.h>
#include <math.h>
#include <dos.h>
/**
* Tanimlar
*/
#define CHAR03 0x03
#define INT_VIDEO 0x10
#define VGA13 0x13
/**
* Yapilar
*/
class POINT2D
{
public:
POINT2D();
POINT2D(int xVal, int yVal);
~POINT2D();
int x;
int y;
};
void POINT2D::POINT2D()
{
// Bos
}
void POINT2D::POINT2D(int xVal, int yVal)
{
x = xVal;
y = yVal;
}
void POINT2D::~POINT2D()
{
// Bos
}
/**
* Global Degiskenler
*/
char far *VGAMAP = (char far *) MK_FP(0xA000, 0);
/**
* Prototip Fonksiyonlar
*/
void setScreen(int screenMode);
void plot(unsigned int x, unsigned char y, unsigned char color);
void plot(POINT2D *p2dPoint, unsigned char color);
void line(int x1, int y1, int x2, int y2, unsigned char color);
void line(POINT2D *p2dPoint1, POINT2D *p2dPoint2, unsigned char color);
/**
* Ana Fonksiyon
*/
void main()
{
// Degisken tanimlari
int x, y, i;
POINT2D myPoint1;
POINT2D myPoint2(40, 40);
myPoint1.x = 30;
myPoint1.y = 30;
// 320x200 256 renk ekran moduna gec
setScreen(VGA13);
// Ekrana birkac ornek nokta bas
plot(30, 40, 14); // Birinci kullanim bicimi
plot(&myPoint1, 10); // Ikinci kullanim bicimi
plot(&myPoint2, 13);
plot(&POINT2D(40, 30), 12); // Ucuncu kullanim bicimi
// Ekrana birkac ornek cizgi cek
// Cizgilerin baslangic ve bitislerini plot ile isaretle
// Cizgi 1
myPoint1.x = 80; myPoint1.y = 150;
myPoint2.x = 213; myPoint2.y = 48;
line(&myPoint1, &myPoint2, 12);
plot(&myPoint1, 15);
plot(&myPoint2, 15);
// Cizgi 2
myPoint1.x = 80; myPoint1.y = 48;
myPoint2.x = 213; myPoint2.y = 150;
line(&myPoint1, &myPoint2, 13);
plot(&myPoint1, 15);
plot(&myPoint2, 15);
// Cizgilerden bir daire cizer
for(i = 0; i < 512; i++)
{
x = 250 + 55 * cos((float)i / 256 * M_PI);
y = 100 + 55 * sin((float)i / 256 * M_PI);
line(250, 100, x, y, 32 + (x & 31));
}
// Bir tusa basilana kadar bekle
getch();
// 80x25 karakter ekran moduna gec
setScreen(CHAR03);
}
/**
* Name : setScreen
* Desc : 0x10 interruptini kullanarak ekran modunu degistirir
* Input : (int)screenMode
*/
void setScreen(int screenMode)
{
union REGS regs;
regs.h.ah = 0;
regs.h.al = screenMode;
int86(INT_VIDEO, ®s, ®s);
}
/**
* Name : plot
* Desc : Parametreden gelen (x,y) koordinatlarina, yine parametreden
* gelen rengi basar.
* Input : (unsigned int)x
* (unsigned char)y
* (unsigned char)color
*/
void plot(unsigned int x, unsigned char y, unsigned char color)
{
/********************************
* Formulun Cikarilisi *
*********************************
* (y * 320) + x => *
* (y * 256) + (y * 64) + x => *
* (y * 2^8) + (y * 2^6) + x => *
* (y << 8) + (y << 6) + x *
********************************/
VGAMAP[(y << 8) + (y << 6) + x] = color;
}
/**
* Name : plot
* Desc : Overloaded plot fonksiyonu. (x,y) koordinatlarini POINT2D
* yapisindaki tek bir degiskenden alir.
* Input : (POINT2D *)p2dPoint
* (unsigned char)color
*/
void plot(POINT2D *p2dPoint, unsigned char color)
{
VGAMAP[(p2dPoint->y << 8) + (p2dPoint->y << 6) + p2dPoint->x] = color;
}
/**
* Name : line
* Desc : Parametreden gelen (x1,y1)-(x2,y2) koordinatlari arasina
* cizgi ceker. <math.h>'a ihtiyac duymaz.
* Bresenham algoritmasi kullanir.
* Input : (int)x1
* (int)y1
* (int)x2
* (int)y2
* (unsigned char)color
*/
void line(int x1, int y1, int x2, int y2, unsigned char color)
{
int dx, dy, x, y, a = 0;
dx = (x1 - x2 > 0) ? x1 - x2 : x2 - x1; // Yatay duzlemdeki genislik
dy = (y1 - y2 > 0) ? y1 - y2 : y2 - y1; // Dikey duzlemdeki uzunluk
if(dx > dy) // Yatay duzlemde genis ise
{
x = x1;
y = y1;
while(1)
{
plot(x, y, color);
a += dy;
if(a >= dx)
{
a -= dx;
y += (y1 > y2) ? -1 : 1;
}
if(x == x2)
break;
x += (x1 > x2) ? -1 : 1;
}
}
else // Dikey duzlemde uzun ise
{
x = x1;
y = y1;
while(1)
{
plot(x, y, color);
a += dx;
if(a >= dy)
{
a -= dy;
x += (x1 > x2) ? -1 : 1;
}
if(y == y2)
break;
y += (y1 > y2) ? -1 : 1;
}
}
}
/**
* Name : line
* Desc : Overloaded line fonksiyonu. (x1,y1) ve (x2,y2) yerine
* POINT2D yapisindaki iki degisken kullanilir.
* Input : (POINT2D *)p2dPoint1
* (POINT2D *)p2dPoint2
* (unsigned char)color
*/
void line(POINT2D *p2dPoint1, POINT2D *p2dPoint2, unsigned char color)
{
line(p2dPoint1->x, p2dPoint1->y, p2dPoint2->x, p2dPoint2->y, color);
}