1
SFML projects / Re: Screenshot Thread
« on: November 14, 2024, 06:06:15 pm »
Analog Clock
Source code: https://gist.github.com/Przemekkkth/3a0c9acae9d8824780a7b9b7bb07fbe2
Additionally I created a function which use Bresenham's line algorithm and help drawing a line between two points with specified width and color:
Source code: https://gist.github.com/Przemekkkth/3a0c9acae9d8824780a7b9b7bb07fbe2
Additionally I created a function which use Bresenham's line algorithm and help drawing a line between two points with specified width and color:
int drawLine(sf::RenderWindow &window, sf::Vector2i point1, sf::Vector2i point2, int lineWidth, sf::Color lineColor)
{
int x0 = point1.x;
int y0 = point1.y;
int x1 = point2.x;
int y1 = point2.y;
int dx = abs(x1 - x0);
int sx, sy;
if (x0 < x1) {
sx = 1;
}
else {
sx = -1;
}
int dy = -abs(y1 - y0);
if (y0 < y1) {
sy = 1;
}
else {
sy = -1;
}
int err = dx + dy;
while (true) {
sf::RectangleShape rect(sf::Vector2f(lineWidth, lineWidth));
rect.setFillColor(lineColor);
rect.setPosition(x0, y0);
window.draw(rect);
if (x0 == x1 && y0 == y1) {
break;
}
int e2 = 2 * err;
if (e2 >= dy) {
err += dy;
x0 += sx;
}
if (e2 <= dx) {
err += dx;
y0 += sy;
}
}
}
{
int x0 = point1.x;
int y0 = point1.y;
int x1 = point2.x;
int y1 = point2.y;
int dx = abs(x1 - x0);
int sx, sy;
if (x0 < x1) {
sx = 1;
}
else {
sx = -1;
}
int dy = -abs(y1 - y0);
if (y0 < y1) {
sy = 1;
}
else {
sy = -1;
}
int err = dx + dy;
while (true) {
sf::RectangleShape rect(sf::Vector2f(lineWidth, lineWidth));
rect.setFillColor(lineColor);
rect.setPosition(x0, y0);
window.draw(rect);
if (x0 == x1 && y0 == y1) {
break;
}
int e2 = 2 * err;
if (e2 >= dy) {
err += dy;
x0 += sx;
}
if (e2 <= dx) {
err += dx;
y0 += sy;
}
}
}