This repository has been archived by the owner on Mar 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kary-tabesh-curve.cpp
160 lines (129 loc) · 5.59 KB
/
kary-tabesh-curve.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// This is a testing proof of concept software that selects every pixel within
// a triangle using the Kary-Tabesh Curve to show that the system is working.
//
// This demo
// Copyright 2018 - Pouya Kary, All Rights Reserved.
// Kary-Tabesh Curve
// Copyright 2011 - Pouya Kary and Armin Tabesh. All Rights Resverd.
//
//
// ─── IMPORTS ────────────────────────────────────────────────────────────────────
//
#ifdef __APPLE__
#include <GLUT/glut.h>
#include <stdlib.h>
#else
#include <GL/glut.h>
#endif
#include <math.h>
#include <time.h>
#include <iostream>
#include <vector>
//
// ─── TYPES ──────────────────────────────────────────────────────────────────────
//
struct Point {
float x;
float y;
};
struct Line {
Point start;
Point end;
};
//
// ─── SETTING CONSTANTS ──────────────────────────────────────────────────────────
//
// window
const int screen_width =
800;
const int screen_height =
500;
// triangle
const struct Point topPosition =
{ 400, 100 };
const struct Point bottomLeftPosition =
{ 300, 400 };
const struct Point bottomRightPosition =
{ 600, 350 };
// trangle edges
const struct Line edge1 =
{ topPosition, bottomLeftPosition };
const struct Line edge2 =
{ bottomLeftPosition, bottomRightPosition };
const struct Line edge3 =
{ bottomRightPosition, topPosition };
const std::vector<Line> edges =
{ edge1, edge2, edge3 };
// origin
const struct Point origin =
{ 0, 0 };
//
// ─── DO LINES INTERSECT ─────────────────────────────────────────────────────────
//
bool lines_are_intesecting( Line a, Line b ) {
const auto first_condition =
( ( ( b.start.x - a.start.x ) * ( a.end.y - a.start.y )
- ( b.start.y - a.start.y ) * ( a.end.x - a.start.x ) )
* ( ( b.end.x - a.start.x ) * ( a.end.y - a.start.y )
- ( b.end.y - a.start.y ) * ( a.end.x - a.start.x ) ) < 0 );
const auto second_condition =
( ( ( a.start.x - b.start.x ) * ( b.end.y - b.start.y )
- ( a.start.y - b.start.y ) * ( b.end.x - b.start.x ) )
* ( ( a.end.x - b.start.x ) * ( b.end.y - b.start.y )
- ( a.end.y - b.start.y ) * ( b.end.x - b.start.x ) ) < 0 );
return first_condition && second_condition;
}
//
// ─── KARY TABESH CURVE TEST ─────────────────────────────────────────────────────
//
bool test_interpolation_using_kary_tabesh_curve ( Point point ) {
const Line test_line =
{ origin, point };
int sign = 1;
for ( auto edge : edges )
if ( lines_are_intesecting( test_line, edge ) )
sign = sign * -1;
return sign == -1;
}
//
// ─── DISPLAY VIEW ───────────────────────────────────────────────────────────────
//
void display ( ) {
glColor4f( 1.0f , 0.0f , 0.0f, 0.3f );
glBegin( GL_POINTS );
for ( float x = 0; x < screen_width; x++ ) {
for ( float y = 0; y < screen_height; y++ ) {
const struct Point point =
{ x, y };
if ( test_interpolation_using_kary_tabesh_curve( point ) )
glVertex2f( point.x, point.y );
}
}
glEnd( );
glFlush( );
}
//
// ─── INIT ───────────────────────────────────────────────────────────────────────
//
void init ( ) {
glClearColor( 0.0, 0.0, 0.0, 1.0 );
glClear( GL_COLOR_BUFFER_BIT );
glColor4f( 1.0, 1.0, 1.0, 1.0 );
glOrtho( 0.f, screen_width, screen_height, 0.f, 0.f, 1.f );
}
//
// ─── MAIN ───────────────────────────────────────────────────────────────────────
//
int main ( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA );
glutInitWindowSize( screen_width, screen_height );
glutInitWindowPosition( 100, 100);
glutCreateWindow( "Kary-Tabesh Curve - Triangle Selector" );
init( );
glutDisplayFunc( display );
glutMainLoop( );
return 0;
}
// ────────────────────────────────────────────────────────────────────────────────