-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBresenham's_line_algorithm.cpp
More file actions
81 lines (66 loc) · 1.54 KB
/
Bresenham's_line_algorithm.cpp
File metadata and controls
81 lines (66 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// main.cpp
// Bresenham's line algorithm
//
// Created by Мария Волкова on 06.11.15.
// Copyright (c) 2015 Маша. All rights reserved.
//
#include <iostream>
#include "math.h"
#define roundf(x) floor(x + 0.5f)
void line( int x1, int y1, int x2, int y2)
{
int dx = (x2 - x1 >= 0 ? 1 : -1);
int dy = (y2 - y1 >= 0 ? 1 : -1);
int lengthX = abs(x2 - x1);
int lengthY = abs(y2 - y1);
int length;
if (lengthX > lengthY)
length = lengthX;
else length = lengthY;
if (length == 0)
{
std :: cout << x1<<" "<< y1 << " ";
}
if (lengthY <= lengthX)
{
// Начальные значения
int x = x1;
int y = y1;
int d = -lengthX;
// Основной цикл
length++;
while(length--)
{
std :: cout << x<<" "<< y <<" ";
x += dx;
d += 2 * lengthY;
if (d > 0) {
d -= 2 * lengthX;
y += dy;
}
}
}
else
{
// Начальные значения
int x = x1;
int y = y1;
int d = - lengthY;
// Основной цикл
length++;
while(length--)
{
std :: cout << x<<" "<< y <<" ";
y += dy;
d += 2 * lengthX;
if (d > 0) {
d -= 2 * lengthY;
x += dx;
}
}
}
}
int main (void)
{ line( 2, 4, 5, 7);
}