Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <allegro.h>
- #include <time.h>
- #include <math.h>
- #define COL_BLACK 0x000000
- #define COL_WHITE 0xFFFFFF
- #define COL_RED 0xFF0000
- #define COL_GREEN 0x00FF00
- #define DIM 400
- #define TR_C 15
- typedef struct{
- int Xmin, Xmax, Ymin, Ymax;
- } rectangle_t;
- typedef struct{
- int x,y;
- } point_t;
- typedef struct{
- point_t pt[3];
- } triangle_t;
- void drawLine(int x0, int y0, int x1, int y1, int col, float m) {
- int x, y;
- float dx = x1 - x0;
- float dy = y1 - y0;
- if (abs(dx) >= abs(dy)) {
- y = y0;
- if (dx > 0){
- for (x = x0; x <= x1; x++)
- putpixel(screen, x, round(m * (x - x0) + y), col);
- }
- else {
- for (x = x0; x >= x1; x--)
- putpixel(screen, x, round(m * (x - x0) + y), col);
- }
- }
- else {
- x = x0;
- if (dy > 0){
- for (y = y0; y <= y1; y++)
- putpixel(screen, round((y - y0) / m + x0), y, col);
- }
- else {
- for (y = y0; y >= y1; y--)
- putpixel(screen, round((y - y0) / m + x0), y, col);
- }
- }
- }
- void swapPoints(point_t* p1, point_t* p2){
- point_t tmp = (*p1);
- (*p1) = (*p2);
- (*p2) = tmp;
- return;
- }
- char getXCoord(int x, rectangle_t rec){
- if (x < rec.Xmin)
- return 1;
- if (x >= rec.Xmin && x <= rec.Xmax)
- return 0;
- if (x > rec.Xmax)
- return 2;
- }
- char getYCoord(int y, rectangle_t rec){
- if (y < rec.Ymin)
- return 8;
- if (y >= rec.Ymin && y <= rec.Ymax)
- return 0;
- if (y > rec.Ymax)
- return 4;
- }
- int getX(int y, float a, float b){
- return round((y - b) / a);
- }
- int getY(int x, float a, float b){
- return round(a * x + b);
- }
- void checkLine(point_t pt1, point_t pt2, rectangle_t rec){
- point_t pp = pt1;
- point_t pk = pt2;
- float dx = pt2.x - pt1.x;
- float dy = pt2.y - pt1.y;
- float a = dy / dx;
- float b = pt1.y - a * pt1.x;
- while(1){
- char cxp = getXCoord(pp.x, rec);
- char cxk = getXCoord(pk.x, rec);
- char cyp = getYCoord(pp.y, rec);
- char cyk = getYCoord(pk.y, rec);
- char cpp = cxp | cyp;
- char cpk = cxk | cyk;
- if ((cpp | cpk) == 0){
- drawLine(pp.x, pp.y, pk.x, pk.y, COL_RED, a);
- break;
- }
- if ((cpp & cpk) != 0){
- drawLine(pp.x, pp.y, pk.x, pk.y, COL_GREEN, a);
- break;
- }
- if (cpp == 0){
- swapPoints(&pp, &pk);
- continue;
- }
- if ((cpp & 4) == 4) {
- int xk = getX(rec.Ymax, a, b);
- drawLine(pp.x, pp.y, xk, rec.Ymax, COL_GREEN, a);
- pp.x = xk;
- pp.y = rec.Ymax;
- swapPoints(&pp, &pk);
- continue;
- }
- if ((cpp & 8) == 8) {
- int xk = getX(rec.Ymin, a, b);
- drawLine(pp.x, pp.y, xk, rec.Ymin, COL_GREEN, a);
- pp.x = xk;
- pp.y = rec.Ymin;
- swapPoints(&pp, &pk);
- continue;
- }
- if ((cpp & 2) == 2) {
- int yk = getY(rec.Xmax, a, b);
- drawLine(pp.x, pp.y, rec.Xmax, yk, COL_GREEN, a);
- pp.x = rec.Xmax;
- pp.y = yk;
- swapPoints(&pp, &pk);
- continue;
- }
- if ((cpp & 1) == 1) {
- int yk = getY(rec.Xmin, a, b);
- drawLine(pp.x, pp.y, rec.Xmin, yk, COL_GREEN, a);
- pp.x = rec.Xmin;
- pp.y = yk;
- swapPoints(&pp, &pk);
- continue;
- }
- }
- }
- void drawTriangle(triangle_t trg, rectangle_t rec){
- checkLine(trg.pt[0], trg.pt[1], rec);
- checkLine(trg.pt[1], trg.pt[2], rec);
- checkLine(trg.pt[0], trg.pt[2], rec);
- }
- int main() {
- srand(time(NULL));
- rectangle_t rec;
- rec.Xmin = 50 + rand() % 50;
- rec.Ymin = 50 + rand() % 50;
- rec.Xmax = (DIM - 200) + rand() % 100;
- rec.Ymax = (DIM - 200) + rand() % 100;
- triangle_t trg[TR_C];
- int i,j;
- for (i = 0; i < TR_C; i++){
- for (j = 0; j < 3; j++){
- trg[i].pt[j].x = rand() % DIM;
- trg[i].pt[j].y = rand() % DIM;
- }
- }
- allegro_init();
- install_keyboard();
- set_color_depth(32);
- set_gfx_mode(GFX_AUTODETECT_WINDOWED, DIM, DIM, 0, 0);
- set_palette(default_palette);
- clear_to_color(screen, COL_BLACK);
- rect(screen, rec.Xmin, rec.Ymin, rec.Xmax, rec.Ymax, COL_WHITE);
- for (i = 0; i < TR_C; i++)
- drawTriangle(trg[i], rec);
- readkey();
- remove_keyboard();
- allegro_exit();
- return 0;
- }
- END_OF_MAIN();
Advertisement
Add Comment
Please, Sign In to add comment