컴퓨터 그래픽스 레포트



대학교때 자료를 찾아보다가 컴퓨터 그래픽스 레포트 했던 자료가 있어서

혹시나 필요하신분께 공개해 드립니다.^^


지금보니 코드가 난잡하기 아주 그냥 그지없군요 ㅎㅎㅎㅎ


뭔소린지 모르겠네요 ㅎㅎ


내용은 컴퓨터 그래픽의 기본인 점/선/원/타원/점선 등의 기본 함수를 사용하는 방법을 구현하는 코드입니다.

결과물은 아래와 같아요


    


소스코드입니다.


#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>


#define ROUND(a) ((int)(a+0.5))

#define SIGNAL_EXIT 4

#define TRUE 1

#define FALSE 0

#define BUFFER 1024

#define MAX_LEVEL 255

#define PI 3.14


#define MASK_BIT 8   // Masking

#define MASK_TYPE 4


#define SCR_WIDTH 100 // Image Size

#define SCR_HEIGHT 100

#define RGB_CODE 3      // RGB Color


#define R 0

#define G 1

#define B 2


//////////////////

// processing part

int menu(void);

int WriteToImagePixel(unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE]);

int GetRGBColor(int palette[RGB_CODE]);

int GetMaskType(char *mask);

void Rotation(int degree, int *x, int *y);


/////////////////

// Draw Line Part

int DrawLine(void);

int GetLinePosition(int *xa, int *ya, int *xb, int *yb);

int lineDDA (int xa, int ya, int xb, int yb, char *mask, int palette[RGB_CODE],

unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE]);


/////////////////

// Draw Circle Part

int DrawCircle(void);

int circleMidpoint(int xCenter, int yCenter, int degree, int *palette,

  unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE]);


void cirlcePlotPoints(int xCenter, int yCenter, int x, int y, int *palette,

 unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE]);


int GetCircleInfo(int *xCenter, int *yCenter, int *degree);


/////////////////

// Draw Ellipse Part

int DrawEllipse(void);

int ellipeMidpoint(int degree, int xCenter, int yCenter, int Rx, int Ry,int *palette,

 unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE]);


void ellipsePlotPoints(int degree, int xCenter, int yCenter, int Rx, int Ry,int *palette,

 unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE]);

int GetEllipseInfo(int *xCenter, int *yCenter, int *Rx, int *Ry, int *degree);

void ellipseSetPoint(int xCenter, int yCenter, int x, int y , int *palette,

unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE]);

void EllipseRotation(int degree, int *Rx, int *Ry, int x, int y);


////////////////

/*  Main Part */


void main()

{

int select;


// Fingure Processing

do{

system("cls");

select = menu();


switch(select) {

case 1:

if(!DrawLine())

printf("Draw Line Failed\n");

break;


case 2:

if(!DrawCircle())

printf("Draw Circle Failed\n");

break;


case 3:

if(!DrawEllipse())

printf("Draw Ellipse Failed\n");

break;

case 4:

printf("Program Exit\n");

break;


default:

printf("Invalide Value Inserted\n");

break;

}


// wait press anykey

getch();


}while(select!=SIGNAL_EXIT);


}


int menu(void)

{

int select;


printf("1. Draw Line \n");

printf("2. Draw Circle\n");

printf("3. Draw Ellipse\n");

printf("4. Exit\n");


printf("Select Menu : ");

scanf("%d",&select);


return select;

}


int GetLinePosition(int *xa, int *ya, int *xb, int *yb)

{

printf("Insert Line Pos xa, ya, xb, yb : ");

scanf("%d %d %d %d",xa,ya,xb,yb);


return TRUE;

}


int DrawLine(void)

{

int j,k; // loop index

int xa=0, ya=0, xb=0, yb=0; // Line Start, End Pos

int palette[RGB_CODE] = {0,0,0}; // Get Color

char mask[MASK_BIT] = {1,1,1,1,1,1,1,1};// Line Type Masking default


unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE];


for(j=0; j<SCR_HEIGHT; j++) {

for(k=0; k<SCR_WIDTH; k++) {

ImagePixel[j][k][R] = 255;

ImagePixel[j][k][G] = 255;

ImagePixel[j][k][B] = 255;

}

}


if(!GetLinePosition(&xa,&ya,&xb,&yb))

return FALSE;


if(!GetRGBColor(palette))

return FALSE;


if(!GetMaskType(mask))

return FALSE;


// Draw Line

if(!lineDDA(xa,ya,xb,yb,mask,palette,ImagePixel))

return FALSE;


return TRUE;

}


int GetMaskType(char *mask)

{

int type; // line type

int i; // loop index

char Type[MASK_TYPE][MASK_BIT] = {  {1,1,1,1,1,1,1,1},

{1,0,1,0,1,0,1,0},

{1,1,0,0,1,1,0,0},

{0,0,0,0,1,1,1,1} };


printf("Type 1 : [ --------------------- ] \n");

printf("Type 2 : [ - - - - - - - - - - - ] \n");

printf("Type 3 : [ --  --  --  --  --  --] \n");

printf("Type 4 : [ ----    ----    ----  ] \n");

printf("Select : ");

scanf("%d",&type);


for(i=0; i<MASK_BIT; i++)

mask[i] = Type[type-1][i];


return TRUE;

}


int GetRGBColor(int *palette)

{

printf("Insert R G B color : ");

scanf("%d %d %d",(palette+0),(palette+1),(palette+2));


return TRUE;

}


int lineDDA(int xa, int ya, int xb, int yb, char *mask, int palette[RGB_CODE],

unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE])

{

int dx = xb - xa;

int dy = yb - ya;

int step, k, cnt;

float xIncrement, yIncrement, x = (float)xa, y = (float)ya;


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

step = abs(dx);

else

step = abs(dy);

xIncrement = dx / (float) step;

yIncrement = dy / (float) step;


for(k=0; k<step; k++) {

cnt = k % MASK_BIT;

x += xIncrement;

y += yIncrement;


int width_pos = abs((int)x);

int height_pos = abs((int)y);


if(mask[cnt]==1) {

ImagePixel[height_pos][width_pos][R] = palette[R];

ImagePixel[height_pos][width_pos][G] = palette[G];

ImagePixel[height_pos][width_pos][B] = palette[B];

}

}


if(WriteToImagePixel(ImagePixel))

printf("Image write success \n");


return TRUE;

}


int WriteToImagePixel(unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE])

{

FILE *fp; // file pointer

char name[BUFFER]; // buffer

int j,k; // loop index

printf("Insert File Name : ");

scanf("%s",name);


fp = fopen(name,"wb");

printf("Name : %s\n",name);

if(fp==NULL) { printf("File open failed!!!\n"); return FALSE; }


fprintf(fp,"P6\n");

fprintf(fp,"%d %d\n",SCR_WIDTH,SCR_HEIGHT);

fprintf(fp,"%d\n",MAX_LEVEL);

for(j=0; j<SCR_HEIGHT; j++) {

for(k=0; k<SCR_WIDTH; k++) {

putc((unsigned char)ImagePixel[j][k][R],fp);

putc((unsigned char)ImagePixel[j][k][G],fp);

putc((unsigned char)ImagePixel[j][k][B],fp);

}

}

fclose(fp);

return TRUE;

}



int DrawCircle(void)

{

int j,k; // loop index

int xCenter = 0, yCenter = 0, degree = 0;

int palette[RGB_CODE] = {0,0,0}; // Get Color

unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE];

// init Image Background color white

for(j=0; j<SCR_HEIGHT; j++) {

for(k=0; k<SCR_WIDTH; k++) {

ImagePixel[j][k][R] = 255;

ImagePixel[j][k][G] = 255;

ImagePixel[j][k][B] = 255;

}

}


if(!GetCircleInfo(&xCenter,&yCenter,&degree))

return FALSE;


if(!GetRGBColor(palette))

return FALSE;


if(!circleMidpoint(xCenter,yCenter,degree,palette,ImagePixel))

return FALSE;


if(WriteToImagePixel(ImagePixel))

printf("Image write success \n");


return TRUE;

}


int GetCircleInfo(int *xCenter, int *yCenter, int *degree)

{

printf("Insert xCenter yCenter degree : ");

scanf("%d %d %d",xCenter,yCenter,degree);


return TRUE;

}


void cirlcePlotPoints(int xCenter, int yCenter, int x, int y,

 int *palette, unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE])

{


ellipseSetPoint(xCenter,yCenter,x,y,palette,ImagePixel);

ellipseSetPoint(xCenter,yCenter,x*(-1),y,palette,ImagePixel);

ellipseSetPoint(xCenter,yCenter,x,y*(-1),palette,ImagePixel);

ellipseSetPoint(xCenter,yCenter,x*(-1),y*(-1),palette,ImagePixel);

ellipseSetPoint(xCenter,yCenter,y,x,palette,ImagePixel);

ellipseSetPoint(xCenter,yCenter,y*(-1),x,palette,ImagePixel);

ellipseSetPoint(xCenter,yCenter,y,x*(-1),palette,ImagePixel);

ellipseSetPoint(xCenter,yCenter,y*(-1),x*(-1),palette,ImagePixel);

}


int circleMidpoint(int xCenter, int yCenter, int degree, int *palette,

  unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE])

{

int x = 0;

int y = degree;

int p = 1 - degree;


cirlcePlotPoints(xCenter, yCenter, x, y, palette, ImagePixel);

while(x < y) {

x++;

if(p < 0)

p += 2 * x + 1;

else {

y--;

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

}


cirlcePlotPoints(xCenter, yCenter, x, y, palette, ImagePixel);

}


return TRUE;

}


int DrawEllipse(void)

{

int j,k; // loop index

int xCenter=0, yCenter=0, Rx=0, Ry=0, degree=0;

int palette[RGB_CODE] = {0,0,0};

unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE];

// init Image Background color white

for(j=0; j<SCR_HEIGHT; j++) {

for(k=0; k<SCR_WIDTH; k++) {

ImagePixel[j][k][R] = 255;

ImagePixel[j][k][G] = 255;

ImagePixel[j][k][B] = 255;

}

}


if(!GetEllipseInfo(&xCenter,&yCenter,&Rx,&Ry,&degree))

return FALSE;


if(!GetRGBColor(palette))

return FALSE;


if(!ellipeMidpoint(degree,xCenter,yCenter,Rx,Ry,palette,ImagePixel))

return FALSE;


if(WriteToImagePixel(ImagePixel))

printf("Image write success \n");


return TRUE;

}



int GetEllipseInfo(int *xCenter, int *yCenter, int *Rx, int *Ry, int *degree)

{

printf("Insert xCenter yCenter Rx Ry degree : ");

scanf("%d %d %d %d %d",xCenter,yCenter,Rx,Ry,degree);


return TRUE;

}


void Rotation(int degree, int *x, int *y)

{

*x = (int)((*x) * cos(degree) - (*y) * sin(degree));

*y = (int)((*x) * sin(degree) + (*y) * cos(degree));

}


int ellipeMidpoint(int degree, int xCenter, int yCenter, int Rx, int Ry, int *palette,

 unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE])

{

int Rx2 = Rx * Rx;

int Ry2 = Ry * Ry;

int twoRx2 = 2 * Rx2;

int twoRy2 = 2 * Ry2;

int p;

int x = 0;

int y = Ry;

int px = 0;

int py = twoRx2 * y;

/* Plot the first set of points */

ellipsePlotPoints(degree, xCenter, yCenter, x, y, palette, ImagePixel);


/* Region1 */

p = ROUND(Ry2 - (Rx2 * Ry) + (0.25 * Rx2));

while (px < py) {

x++;

px += twoRy2;

if (p < 0)

p += Ry2 + px;

else {

y--;

py -= twoRx2;

p += Ry2 + px - py;

}

ellipsePlotPoints(degree, xCenter, yCenter, x, y, palette, ImagePixel);

}


/* Region2 */

p = ROUND(Ry2*(x+0.5)*(x+0.5) + Rx2*(y-1)*(y-1) - Rx2*Ry2);

while (y > 0) {

y--;

py -= twoRx2;

if (p > 0)

p += Rx2 - py;

else {

x++;

px += twoRy2;

p += Rx2 - py + px;

}

ellipsePlotPoints(degree, xCenter, yCenter, x, y, palette, ImagePixel);

}


return TRUE;

}


void ellipsePlotPoints(int degree ,int xCenter, int yCenter, int x, int y,int *palette,

 unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE])

{

int Rx=0, Ry=0;


EllipseRotation(degree, &Rx, &Ry, x, y);

ellipseSetPoint(xCenter,yCenter,Rx,Ry,palette,ImagePixel);


EllipseRotation(degree, &Rx, &Ry, x*(-1), y);

ellipseSetPoint(xCenter,yCenter,Rx,Ry,palette,ImagePixel);

EllipseRotation(degree, &Rx, &Ry, x, y*(-1));

ellipseSetPoint(xCenter,yCenter,Rx,Ry,palette,ImagePixel);

EllipseRotation(degree, &Rx, &Ry, x*(-1), y*(-1));

ellipseSetPoint(xCenter,yCenter,Rx,Ry,palette,ImagePixel);

}


void EllipseRotation(int degree, int *Rx, int *Ry, int x, int y)

{

double radian = degree*PI/180;

*Rx = (int)(x * cos(radian) - y * sin(radian));

*Ry = (int)(x * sin(radian) + y * cos(radian));

}

void ellipseSetPoint(int xCenter, int yCenter, int x, int y , int *palette,

unsigned char ImagePixel[SCR_HEIGHT][SCR_WIDTH][RGB_CODE])

{

ImagePixel[yCenter+y][xCenter+x][R] = palette[R];

ImagePixel[yCenter+y][xCenter+x][G] = palette[G];

ImagePixel[yCenter+y][xCenter+x][B] = palette[B];

}