DDA

#include <iostream>
#include <graphics.h>
using namespace std;

int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, (char *)"");
    float x1, y1, x2, y2;

    cout << "Enter the value of x1 and y1 : ";
    cin >> x1 >> y1;
    cout << "Enter the value of x2 and y2: ";
    cin >> x2 >> y2;

    int dx, dy;
    dy = y2 - y1;
    dx = x2 - x1;
    float m = dy / dx;

    float xi, yi;

    while (x1 <= x2 && y1 <= y2)
    {
        if (dx >= dy)
        {
            xi = x1 + 1;
            yi = y1 + m;
        }
        else
        {
            xi = x1 + (1 / m);
            yi = y1 + 1;
        }
        putpixel(x1, y1, WHITE);
        x1 = xi;
        y1 = yi;
        delay(30);
    }

    getch();
    closegraph();
    return 0;
}

Bresenham

#include <iostream>
#include <graphics.h>
using namespace std;
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, (char *)"");
    float x1, x2, y1, y2, xi, yi, Po, Pi, m;
    int dy, dx;
    cout << "Enter x1 and y1: ";
    cin >> x1 >> y1;
    cout << "Enter x2 and y2: ";
    cin >> x2 >> y2;

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

    Po = 2 * dy - dx;
    while (x1 <= x2 && y1 <= y2)
    {
        if (m < 1)
        {
            if (Po < 0)
            {
                xi = x1 + 1;
                yi = y1;
                Pi = Po + 2 * dy;
            }
            else
            {
                xi = x1 + 1;
                yi = y1 + 1;
                Pi = Po + 2 * dy - 2 * dx;
            }
        }
        else
        {
            if (Po < 0)
            {
                xi = x1;
                yi = y1 + 1;
                Pi = Po + 2 * dx;
            }
            else
            {
                xi = x1 + 1;
                yi = y1 + 1;
                Pi = Po + 2 * dx - 2 * dy;
            }
        }
        putpixel(x1, y1, WHITE);
        x1 = xi;
        y1 = yi;
        Po = Pi;
        delay(30);
    }
    getch();
    closegraph();
}

Circle

#include <iostream>
#include <graphics.h>
using namespace std;
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, (char *)"");
    double x, y, r, d;
    cout << "Enter radius:";
    cin >> r;
    x = 0;
    y = r;
    d = 1.25 - r;
    while (x < y)
    {
        putpixel(200 + x, 200 + y, WHITE);
        putpixel(200 + y, 200 + x, WHITE);
        putpixel(200 - x, 200 + y, WHITE);
        putpixel(200 - y, 200 + x, WHITE);
        putpixel(200 + x, 200 - y, WHITE);
        putpixel(200 + y, 200 - x, WHITE);
        putpixel(200 - x, 200 - y, WHITE);
        putpixel(200 - y, 200 - x, WHITE);

        if (d < 0)
        {
            x++;
            d = d + 2 * x + 1;
        }
        else
        {
            x++;
            y--;
            d = d + 2 * x - 2 * y + 1;
        }
    }
    getch();
    closegraph();
    return 0;
}

Ellipse

#include <iostream>
#include <graphics.h>
using namespace std;

int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, (char *)"");
    int rx, ry, x, y, dx, dy, P1, P2;
    cout << "Enter x-radius and y-radius: ";
    cin >> rx >> ry;
    x = 0;
    y = ry;
    dx = 2 * ry * ry * x;
    dy = 2 * rx * rx * y;
    P1 = ry * ry - rx * rx * ry + (1 / 4) * rx * rx;

    while (dx < dy)
    {
        putpixel(200 + x, 200 + y, WHITE);
        putpixel(200 - x, 200 + y, WHITE);
        putpixel(200 + x, 200 - y, WHITE);
        putpixel(200 - x, 200 - y, WHITE);
        if (P1 < 0)
        {
            x++;
            dx = 2 * ry * ry * x;
            P1 = P1 + dx + ry * ry;
        }
        else
        {
            x++;
            y--;
            dx = 2 * ry * ry * x;
            dy = 2 * rx * rx * y;
            P1 = P1 + dx - dy + ry * ry;
        }
    }

    P2 = ry * ry * (x + 0.5) * (x + 0.5) + rx * rx * (y - 1) * (y - 1) - rx * rx * ry * ry;
    while (y > 0)
    {
        putpixel(200 + x, 200 + y, WHITE);
        putpixel(200 - x, 200 + y, WHITE);
        putpixel(200 + x, 200 - y, WHITE);
        putpixel(200 - x, 200 - y, WHITE);
        if (P2 > 0)
        {
            y--;
            dy = 2 * rx * rx * y;
            P2 = P2 - dy + rx * rx;
        }
        else
        {
            x++;
            y--;
            dx = 2 * ry * ry * x;
            dy = 2 * rx * rx * y;
            P2 = P2 + dx - dy + rx * rx;
        }
    }
    getch();
    closegraph();
}

Translation Scaling Rotation

#include <iostream>
#include <graphics.h>
#include <math.h>
#define PI 3.14159265

using namespace std;
int main()
{
    int gd = DETECT, gm;
    initwindow(1000, 500, "Window");

    int vertex, i, k = 1;

    cout << "Enter number of vertices for polygon : ";
    cin >> vertex;
    int size = 2 * (vertex + 1); // size - total no of (x,y) coordinates
    int points[size + 2];
    for (i = 0; i < size - 2; i = i + 2)
    {
        cout << "x and y co-ordinates for " + to_string(k) + " point : \\n";
        cin >> points[i] >> points[i + 1];
        k++;
    }

    // last x coordinate = x1
    points[size - 2] = points[0];
    // last y coordinate = y1
    points[size - 1] = points[1];

    cout << "\\nOG ARRAY :";
    for (int i = 0; i < size; i += 2)
    {
        cout << "(" << points[i] << "," << points[i + 1] << ") ";
    }

    drawpoly(size / 2, points);

    int ch;
    int tx = 0, ty = 0;
    int sx = 1, sy = 1;
    double theta = 0;
    double cos_theta = 0, sin_theta = 0;

    double rad = 0;

    cout << "\\n1 - TRANSLATION\\n2 - SCALING\\n3 - ROTATION\\n0 - End\\nEnter : ";
    cin >> ch;
    switch (ch)
    {

    case 1:
        // TRANSLATION

        cout << "\\nEnter Tx  and Ty : ";
        cin >> tx, ty;
        // int points[] = {320, 150, 420, 300, 250, 300, 320, 150};
        for (i = 0; i < size; i = i + 2)
        {
            points[i] = points[i] + tx;
            points[i + 1] = points[i + 1] + ty;
        }

        outtextxy(100, 5, (char *)"TRANSLATION");

        cout << "\\nAFTER TRANSLATION (new coordinates):";
        for (int i = 0; i < size; i += 2)
        {
            cout << "(" << points[i] << "," << points[i + 1] << ") ";
        }

        setcolor(RED);
        drawpoly(size / 2, points);
        break;

    case 2:
        // SCALING

        cout << "\\nEnter Sx  and Sy : ";
        cin >> sx, sy;
        for (i = 0; i < size; i = i + 2)
        {
            points[i] = points[i] * sx;
            points[i + 1] = points[i + 1] * sy;
        }

        outtextxy(100, 5, (char *)"SCALING");

        cout << "\\nAFTER SCALING (new coordinates):";
        for (int i = 0; i < size; i += 2)
        {
            cout << "(" << points[i] << "," << points[i + 1] << ") ";
        }

        setcolor(YELLOW);
        drawpoly(size / 2, points);
        break;

    case 3:
        // ROTATION
        // setviewport(300, 100, getmaxx(), getmaxy(), 1);
        cout << "\\nEnter theta: ";
        cin >> theta;
        rad = theta * PI / 180;
        cos_theta = cos(rad);
        sin_theta = sin(rad);
        cout << sin_theta << " " << cos_theta;

        for (i = 0; i < size; i = i + 2)
        {
            points[i] = points[i] * cos_theta - points[i + 1] * sin_theta;
            points[i + 1] = points[i] * sin_theta + points[i + 1] * cos_theta;
        }

        outtextxy(100, 5, (char *)"ROTATION");

        cout << "\\nAFTER ROTATION (new coordinates):";

        for (int i = 2; i < size; i += 2)
        {
            cout << "(" << points[i] << "," << points[i + 1] << ") ";
        }

        setcolor(BLUE);
        drawpoly(size / 2, points);
        break;
    }

    getch();
    closegraph();
    return 0;
}

Reflection Shear

#include <iostream>
#include <graphics.h>
#include <conio.h>
using namespace std;

int reflect(int option, int vertex[])
{
    int temp[8];
    for (int i = 0; i < 8; i++)
    {
        if (i % 2 == 0 || i == 0)
        {
            if (option == 1)
                temp[i] = vertex[i] * (-1) + 300;
            else if (option == 2)
                temp[i] = vertex[i] * (1) + 300;
            else if (option == 3)
                temp[i] = vertex[i] * (-1) + 300;
            else if (option == 4)
                temp[i] = vertex[i + 1] * (1) + 300;
            else if (option == 5)
                temp[i] = vertex[i + 1] * (-1) + 300;
        }
        else
        {
            if (option == 1)
                temp[i] = vertex[i] * (1) + 200;
            else if (option == 2)
                temp[i] = vertex[i] * (-1) + 200;
            else if (option == 3)
                temp[i] = vertex[i] * (-1) + 200;
            else if (option == 4)
                temp[i] = vertex[i - 1] * (1) + 200;
            else if (option == 5)
                temp[i] = vertex[i - 1] * (-1) + 200;
        }
    }

    drawpoly(4, temp);
}

int shear(int shx, int shy, int vertex[])
{
    int temp[8];
    for (int i = 0; i < 8; i++)
    {
        if (i % 2 == 0 || i == 0)
            temp[i] = vertex[i] + vertex[i + 1] * shx;
        else
            temp[i] = vertex[i - 1] * shy + vertex[i];
        // cout<<temp[i]<<"\\t";
    }

    drawpoly(4, temp);
}

int main()
{
    int gd = DETECT, gm;
    // initgraph(&gd, &gm, (char*)"");
    initwindow(1000, 500, "Window");
    int sizeT = (3 + 1) * 2;
    int vertex[sizeT];

    int temp, i;

    i = 1;
    do
    {
        cout << "Geometric Transformation Operations:\\n1.Reflect\\n2.Shear\\n3.Exit\\nEnter your decision: ";
        cin >> temp;

        cout << "Enter coordinates: \\n";
        for (int i = 0; i < sizeT; i++)
            cin >> vertex[i];

        switch (temp)
        {
        case 1:
            cout << "Reflection\\n";
            int option;
            cout << "1.Y-axis\\t2.X-axis\\t3.Origin\\t4.y=x\\t5.y=-x\\tEnter an option: \\n";
            cin >> option;

            setcolor(RED);
            reflect(option, vertex);

            for (int i = 0; i < sizeT; i++)
            {
                if (i % 2 == 0 || i == 0)
                    vertex[i] += 300;
                else
                    vertex[i] += 200;
            }
            setcolor(WHITE);
            drawpoly(4, vertex);
            break;

        case 2:
            cout << "Shearing\\n";
            int shx, shy;
            cout << "Enter shearing parameters: \\n";
            cin >> shx >> shy;

            setcolor(WHITE);
            drawpoly(4, vertex);

            setcolor(YELLOW);
            shear(shx, shy, vertex);
            break;

        case 3:
            cout << "Exiting...";
            break;

        default:
            cout << "Invalid Input\\n";
        }

    } while (temp != 3);

    getch();
    closegraph();
    return 0;
}

Floodfill