Last active
October 8, 2017 14:23
-
-
Save ceres-c/e2f4855ceb4ecc37c5dd744fe759063d to your computer and use it in GitHub Desktop.
(Uni homework) Draw filled shapes on a canvas in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define NUMROW 40 | |
#define NUMCOL 40 | |
void drawCircle (char *canvas, int r, int xCenter, int yCenter); | |
void drawEllipse (char *canvas, int width, int height, int xCenter, int yCenter); | |
void drawRhombus (char *canvas, int a, int b, int xCenter, int yCenter); | |
void fillEmpty (char *canvas, int nRow, int nCol); | |
void drawLine (char *canvas, int nRow, int nCol, int x1, int x2, int y); | |
void drawPixel (char *canvas, int nRow, int nCol, int x, int y); | |
void printCanvas (char *canvas, int nRow, int nCol); | |
int main () | |
{ | |
char *pCanvas = (char*) malloc(NUMROW * NUMCOL* sizeof(char)); | |
if (!pCanvas) | |
{ | |
printf ("Errore nell'allocazione della memoria\n"); | |
exit (EXIT_FAILURE); | |
} | |
//Empty out the canvas | |
fillEmpty (pCanvas, NUMROW, NUMCOL); | |
//Draw a circle | |
drawCircle (pCanvas, 15, 20, 20); | |
//Print the result | |
printCanvas (pCanvas, NUMROW, NUMCOL); | |
//Empty out the canvas | |
fillEmpty (pCanvas, NUMROW, NUMCOL); | |
//Draw an ellipse | |
drawEllipse (pCanvas, 15, 10, 20, 20); | |
//Print the result | |
printCanvas (pCanvas, NUMROW, NUMCOL); | |
//Empty out the canvas | |
fillEmpty (pCanvas, NUMROW, NUMCOL); | |
//Draw a Rhombus | |
drawRhombus (pCanvas, 15, 10, 20, 20); | |
//Print the result | |
printCanvas (pCanvas, NUMROW, NUMCOL); | |
exit (EXIT_SUCCESS); | |
} | |
void drawRhombus (char *canvas, int a, int b, int xCenter, int yCenter) | |
{ | |
// a is the horizontal SEMIaxis, b the vertical SEMIaxis | |
int x = a; | |
for (int y = 0; y <= b; y++) | |
{ | |
while (a * y + (x - a) * b > 0) | |
x--; | |
drawLine (canvas, NUMROW, NUMCOL, xCenter - x, xCenter + x, yCenter + y); | |
drawLine (canvas, NUMROW, NUMCOL, xCenter - x, xCenter + x, yCenter - y); | |
} | |
} | |
void drawEllipse (char *canvas, int a, int b, int xCenter, int yCenter) | |
{ | |
// a is the horizontal SEMIaxis, b the vertical SEMIaxis | |
int a2 = a * a, b2 = b * b, a2b2 = a2 * b2; | |
int x = a; | |
for (int y = 0; y <= b; y++) | |
{ | |
while (x * x * b2 + y * y * a2 > a2b2) | |
x--; | |
drawLine (canvas, NUMROW, NUMCOL, xCenter - x, xCenter + x, yCenter + y); | |
drawLine (canvas, NUMROW, NUMCOL, xCenter - x, xCenter + x, yCenter - y); | |
} | |
} | |
void drawCircle (char *canvas, int r, int xCenter, int yCenter) | |
{ | |
int r2 = r * r; | |
int x = r; | |
for (int y = 0; y <= r; y++) | |
{ | |
while (x * x + y * y > r2) | |
x--; | |
drawLine (canvas, NUMROW, NUMCOL, xCenter - x, xCenter + x, yCenter + y); | |
drawLine (canvas, NUMROW, NUMCOL, xCenter - x, xCenter + x, yCenter - y); | |
} | |
} | |
void fillEmpty (char *canvas, int nRow, int nCol) | |
{ | |
for (int i = 0; i < nRow*nCol; i++) | |
*canvas++ = ' '; | |
} | |
void drawLine (char *canvas, int nRow, int nCol, int x1, int x2, int y) | |
{ | |
if (x1 < 0 || x2 < 0 || x1 > nCol || x2 > nCol || y < 0 || y > nRow || x1 > x2) | |
return; | |
for (int i = x1; i <= x2 && i < nCol; i++) | |
*(canvas + y * nCol + i) = '*'; | |
} | |
void printCanvas (char *canvas, int nRow, int nCol) | |
{ | |
for (int i = 0; i < NUMROW*NUMCOL; i++) | |
{ | |
if (i != 0 && i % NUMCOL == 0) printf ("\n"); | |
printf ("%c ", *canvas++); | |
} | |
printf ("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment