0% found this document useful (0 votes)
20 views2 pages

Drawing a Chessboard in C++ Graphics

Uploaded by

Sumit Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

Drawing a Chessboard in C++ Graphics

Uploaded by

Sumit Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <graphics.

h>
#include <iostream>

// Function to initialize the graphics mode


void initGraphics() {
int gd = DETECT, gm;
initgraph(&gd, &gm,(char*) "");
}
void drawDDALine(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
int steps = std::max(abs(dx), abs(dy));

float xIncrement = dx / (float)steps;


float yIncrement = dy / (float)steps;

float x = x1;
float y = y1;

for (int i = 0; i <= steps; i++) {


putpixel(int(x + 0.5), int(y + 0.5), WHITE);
x += xIncrement;
y += yIncrement;
}
}
void drawChessBoard(int startX, int startY, int squareSize) {
int boardSize = 8;
int endX = startX + squareSize * boardSize;
int endY = startY + squareSize * boardSize;
// Draw vertical lines
for (int i = 0; i <= boardSize; i++) {
drawDDALine(startX + i * squareSize, startY, startX + i * squareSize, endY);
}

// Draw horizontal lines


for (int i = 0; i <= boardSize; i++) {
drawDDALine(startX, startY + i * squareSize, endX, startY + i * squareSize);
}
}
int main() {
initGraphics(); // Initialize the graphics system

int startX = 50; // Starting X coordinate of the chessboard


int startY = 50; // Starting Y coordinate of the chessboard
int squareSize = 50; // Size of each square

drawChessBoard(startX, startY, squareSize);

getch(); // Wait for a key press


closegraph(); // Close the graphics mode
return 0;
}

You might also like