Untitled

 avatar
unknown
plain_text
5 months ago
13 kB
3
Indexable
///// CIRCLE DDA ////

#include <GL/glut.h>
#include <vector>
#include <algorithm>
#include <cmath>

void init() {
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(0, 500, 0, 500);
}

void setPixel(int x, int y, float r, float g, float b) {
    glColor3f(r, g, b);
    glBegin(GL_POINTS);
    glVertex2i(x, y);
    glEnd();
    glFlush();
}

// Function to fill between intersections for each scan line
void scanLineFill(int y, int xc, int yc, int r, float fillColor[3]) {
    std::vector<int> intersections;

    // Calculate the x-coordinates of the intersections for the current y scan line
    int y_diff = y - yc;
    if (y_diff * y_diff <= r * r) {
        int x_offset = (int)sqrt(r * r - y_diff * y_diff);  // Pythagoras' theorem
        intersections.push_back(xc - x_offset);  // Left intersection
        intersections.push_back(xc + x_offset);  // Right intersection
    }

    // Fill between pairs of intersections
    for (size_t i = 0; i < intersections.size(); i += 2) {
        for (int x = intersections[i]; x <= intersections[i + 1]; x++) {
            setPixel(x, y, fillColor[0], fillColor[1], fillColor[2]);
        }
    }
}

// Function to draw the circle outline
void drawCircle(int xc, int yc, int r) {
    glColor3f(1.0, 0.0, 0.0);  // Red color for the circle outline
    glBegin(GL_LINE_LOOP);
    for (int i = 0; i < 360; i++) {
        float theta = i * 3.14159f / 180.0f;  // Convert degree to radian
        int x = xc + r * cos(theta);
        int y = yc + r * sin(theta);
        glVertex2i(x, y);
    }
    glEnd();
    glFlush();
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);

    int xc = 250, yc = 250, r = 100;  // Circle center (250, 250) and radius 100
    drawCircle(xc, yc, r);

    float fillColor[] = {0.0, 0.0, 1.0};  // Blue color for filling

    // Scan line fill for the circle
    for (int y = yc - r; y <= yc + r; y++) {
        scanLineFill(y, xc, yc, r, fillColor);
    }
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Scan Line Filling - Circle");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}





/// SQUARE ///

#include <GL/glut.h>
#include <vector>
#include <algorithm>

void init() {
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(0, 500, 0, 500);
}

void setPixel(int x, int y, float r, float g, float b) {
    glColor3f(r, g, b);
    glBegin(GL_POINTS);
    glVertex2i(x, y);
    glEnd();
    glFlush();
}

void scanLineFill(int y, const std::vector<std::pair<int, int>>& edges, float fillColor[3]) {
    std::vector<int> intersections;

    for (size_t i = 0; i < edges.size(); i++) {
        int x1 = edges[i].first, y1 = edges[i].second;
        int x2 = edges[(i + 1) % edges.size()].first, y2 = edges[(i + 1) % edges.size()].second;

        if ((y1 <= y && y2 > y) || (y2 <= y && y1 > y)) {
            int x = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
            intersections.push_back(x);
        }
    }

    std::sort(intersections.begin(), intersections.end());

    for (size_t i = 0; i < intersections.size(); i += 2) {
        for (int x = intersections[i]; i + 1 < intersections.size() && x <= intersections[i + 1]; x++) {
            setPixel(x, y, fillColor[0], fillColor[1], fillColor[2]);
        }
    }
}

void drawPolygon() {
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINE_LOOP);
    glVertex2i(150, 150);
    glVertex2i(350, 150);
    glVertex2i(350, 350);
    glVertex2i(150, 350);
    glEnd();
    glFlush();
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    drawPolygon();

    float fillColor[] = {0.0, 0.0, 1.0};
    std::vector<std::pair<int, int>> edges = {{150, 150}, {350, 150}, {350, 350}, {150, 350}};

    for (int y = 150; y <= 350; y++) {
        scanLineFill(y, edges, fillColor);
    }
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Scan Line Filling - Square");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}



/// TRIANGLE ///
#include <GL/glut.h>

void init() {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    gluOrtho2D(0, 500, 0, 500);
}

void setPixel(int x, int y, float r, float g, float b) {
    glColor3f(r, g, b);
    glBegin(GL_POINTS);
    glVertex2i(x, y);
    glEnd();
    glFlush();
}

void scanLineFill(int y, int edges[][2], int edgeCount, float fillColor[3]) {
    int intersections[10], count = 0;

    // Find intersections for the current scan line
    for (int i = 0; i < edgeCount; i++) {
        int x1 = edges[i][0], y1 = edges[i][1];
        int x2 = edges[(i + 1) % edgeCount][0], y2 = edges[(i + 1) % edgeCount][1];

        if ((y1 <= y && y2 > y) || (y2 <= y && y1 > y)) {
            intersections[count++] = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
        }
    }

    // Sort intersections
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - i - 1; j++) {
            if (intersections[j] > intersections[j + 1]) {
                int temp = intersections[j];
                intersections[j] = intersections[j + 1];
                intersections[j + 1] = temp;
            }
        }
    }

    // Fill pixels between pairs of intersections
    for (int i = 0; i < count; i += 2) {
        for (int x = intersections[i]; x <= intersections[i + 1]; x++) {
            setPixel(x, y, fillColor[0], fillColor[1], fillColor[2]);
        }
    }
}

void drawPolygon() {
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINE_LOOP);
    glVertex2i(250, 400);
    glVertex2i(150, 200);
    glVertex2i(350, 200);
    glEnd();
    glFlush();
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    drawPolygon();

    float fillColor[] = {0.0, 0.0, 1.0};
    int edges[][2] = {{250, 400}, {150, 200}, {350, 200}};
    int edgeCount = 3;

    for (int y = 200; y <= 400; y++) {
        scanLineFill(y, edges, edgeCount, fillColor);
    }
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Scan Line Fill - Triangle");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}



/// DR PLUS DDA LINE ///
#include <GL/glut.h>
#include <iostream>
using namespace std;
int x1, x2, y1, y2;

void drawpixel(int x, int y) {
    glBegin(GL_POINTS);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex2i(x, y);
    glEnd();
}

void init() {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, 500, 0, 500);
}

void drawline(int x1, int x2, int y1, int y2) {
    int dx, dy, i, e;
    int incx = 1, incy = 1, inc1, inc2;
    int x, y;

    dx = x2 - x1;
    dy = y2 - y1;

    if (dx < 0) dx = -dx;
    if (dy < 0) dy = -dy;
    if (x2 < x1) incx = -1;
    if (y2 < y1) incy = -1;

    x = x1;
    y = y1;

    if (dx > dy) {
        drawpixel(x, y);
        e = 2 * dy - dx;
        inc1 = 2 * (dy - dx);
        inc2 = 2 * dy;

        for (i = 0; i < dx; i++) {
            if (e >= 0) {
                y += incy;
                e += inc1;
            } else {
                e += inc2;
            }
            x += incx;
            drawpixel(x, y);
        }
    } else {
        drawpixel(x, y);
        e = 2 * dx - dy;
        inc1 = 2 * (dx - dy);
        inc2 = 2 * dx;

        for (i = 0; i < dy; i++) {
            if (e >= 0) {
                x += incx;
                e += inc1;
            } else {
                e += inc2;
            }
            y += incy;
            drawpixel(x, y);
        }
    }
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    drawline(150,150,120,150);
    drawline(150,120,150,150);
    drawline(120,120,150,170);
    drawline(120,150,170,170);
    drawline(150,150,170,200);
    drawline(150,170,200,200);
    drawline(170,170,200,170);
    drawline(170,200,170,170);
    drawline(200,200,170,150);
    drawline(200,170,150,150);
    drawline(170,170,150,120);
    drawline(170,150,120,120);
    glFlush();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(400, 400);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Home");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}


/// CATERPILLAR //

#include <GL/glut.h>

void setPixel(int x, int y)
{
  glBegin(GL_POINTS);
  glVertex2i(x, y);
  glEnd();
}

void drawCircle(int xc, int yc, int r)
{
  int x = 0, y = r;
  int d = 3 - 2 * r;

  while (y >= x)
  {
    setPixel(xc + x, yc + y);
    setPixel(xc - x, yc + y);
    setPixel(xc + x, yc - y);
    setPixel(xc - x, yc - y);
    setPixel(xc + y, yc + x);
    setPixel(xc - y, yc + x);
    setPixel(xc + y, yc - x);
    setPixel(xc - y, yc - x);

    x++;
    if (d > 0)
    {
      y--;
      d = d + 4 * (x - y) + 10;
    }
    else
    {
      d = d + 4 * x + 6;
    }
  }
}

void drawCaterpillar()
{
  glClear(GL_COLOR_BUFFER_BIT);

  glColor3f(0, 0, 0);
  drawCircle(100, 200, 50);

  glColor3f(0, 0, 0);
  drawCircle(170, 200, 50);

  glColor3f(1, 0, 0);
  drawCircle(240, 200, 50);
  drawCircle(310, 200, 50);
  drawCircle(380, 200, 50);

  glColor3f(0, 0, 0);
  glBegin(GL_LINES);
  glVertex2i(80, 250);
  glVertex2i(50, 300);

  glVertex2i(120, 250);
  glVertex2i(150, 300);
  glEnd();

  drawCircle(50, 300, 10);
  drawCircle(150, 300, 10);

  drawCircle(85, 215, 5);
  drawCircle(115, 215, 5);

  glFlush();
}

void init()
{
  glClearColor(1.0, 1.0, 1.0, 1.0);
  glColor3f(0.0, 0.0, 0.0);
  glPointSize(2.0);
  gluOrtho2D(0.0, 500.0, 0.0, 500.0);
}

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(500, 500);
  glutCreateWindow("Caterpillar using Bresenham's Circle Algorithm");
  init();
  glutDisplayFunc(drawCaterpillar);
  glutMainLoop();
  return 0;
}



/// CLIPPING ///
#include <GL/glut.h>

GLfloat xMin = -0.5, xMax = 0.5, yMin = -0.5, yMax = 0.5;
GLfloat x1 = -0.7, y1 = -0.3, x2 = 0.6, y2 = 0.9;

int Left = 1, Right = 2, Bot = 4, Top = 8;
int C1, C2;
int Clip_Flag = 0;

int Get_Code(GLfloat x, GLfloat y) {
    int Code = 0;
    if (x < xMin) Code = Code | Left;
    if (x > xMax) Code = Code | Right;
    if (y < yMin) Code = Code | Bot;
    if (y > yMax) Code = Code | Top;
    return Code;
}

void Clip() {
    int C;
    GLfloat x, y;
    if (C1) C = C1;
    else C = C2;

    if (C & Left) {
        x = xMin;
        y = y1 + (y2 - y1) * ((xMin - x1) / (x2 - x1));
    }

    if (C & Right) {
        x = xMax;
        y = y1 + (y2 - y1) * ((xMax - x1) / (x2 - x1));
    }

    if (C & Bot) {
        y = yMin;
        x = x1 + (x2 - x1) * ((yMin - y1) / (y2 - y1));
    }

    if (C & Top) {
        y = yMax;
        x = x1 + (x2 - x1) * ((yMax - y1) / (y2 - y1));
    }

    if (C == C1) {
        x1 = x;
        y1 = y;
    } else {
        x2 = x;
        y2 = y;
    }
}

void Draw() {
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_LINE_LOOP);
    glVertex2f(xMin, yMin);
    glVertex2f(xMax, yMin);
    glVertex2f(xMax, yMax);
    glVertex2f(xMin, yMax);
    glEnd();

    glColor3f(1, 0, 0);
    glBegin(GL_LINES);
    glVertex2f(x1, y1);
    glVertex2f(x2, y2);
    glEnd();

    while (1 & Clip_Flag == 1) {
        C1 = Get_Code(x1, y1);
        C2 = Get_Code(x2, y2);
        if ((C1 | C2) == 0) break;
        else if ((C1 & C2) != 0) break;
        else Clip();
    }

    glFlush();
}

void Key(unsigned char ch, int x, int y) {
    Clip_Flag = 1;
    glutPostRedisplay();
}

int main(int argc, char* argv[]) {
    glutInit(&argc, argv);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutCreateWindow("COHEN SUTHERLAND LINE CLIPPING ALGORITHM");
    glutDisplayFunc(Draw);
    glutKeyboardFunc(Key);
    glutMainLoop();
    return 0;
}
Editor is loading...
Leave a Comment