This repository was archived by the owner on Apr 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcartesian.cpp
More file actions
121 lines (104 loc) · 2.65 KB
/
cartesian.cpp
File metadata and controls
121 lines (104 loc) · 2.65 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
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
#ifndef _CARTESIAN_CPP_
#define _CARTESIAN_CPP_
#include "cartesian.h"
Cartesian::Cartesian() :
function(0),
x_ratio(1),
y_ratio(1)
{
};
void Cartesian::draw_to(Drawable* drawable)
{
if(drawable)
{
draw_axis(drawable);
if(function)
{
float from = function->from(), to = function->to(), step = function->step();
drawable->begin_path();
drawable->move_to(transfer_x(from), transfer_y(function->calculate(from)));
for(float i = from; i <= to; i += step)
{
drawable->line_to(transfer_x(i), transfer_y(function->calculate(i)));
}
drawable->end_path();
}
}
};
void Cartesian::add_function(Function* _function)
{
function = _function;
};
void Cartesian::draw_axis(Drawable* drawable)
{
draw_x(drawable, -15, 15);
draw_y(drawable, -15, 15);
draw_x_serif(drawable, -15, 15, 1);
draw_y_serif(drawable, -15, 15, 1);
};
void Cartesian::draw_x(Drawable* drawable, float from, float to)
{
drawable->begin_path();
drawable->move_to(from, 0);
drawable->line_to(to, 0);
// // x-arrow
// drawable->line_to(0.01, 0.49);
// drawable->move_to(0, 0.5);
// drawable->line_to(-0.01, 0.49);
drawable->move_to(0, 0);
drawable->end_path();
};
void Cartesian::draw_y(Drawable* drawable, float from, float to)
{
drawable->begin_path();
drawable->move_to(0, from);
drawable->line_to(0, to);
// // y-arrow
// drawable->line_to(0.49, -0.01);
// drawable->move_to(0.5, 0);
// drawable->line_to(0.49, 0.01);
drawable->move_to(0, 0);
drawable->end_path();
};
void Cartesian::draw_x_serif(Drawable* drawable, float from, float to, float step)
{
drawable->begin_path();
for(float i = from; i <= to; i += step)
{
drawable->move_to(transfer_x(i), -0.1);
drawable->line_to(transfer_x(i), 0.1);
}
drawable->move_to(0, 0);
drawable->end_path();
};
void Cartesian::draw_y_serif(Drawable* drawable, float from, float to, float step)
{
drawable->begin_path();
for(float i = from; i <= to; i += step)
{
drawable->move_to(-0.1, transfer_y(i));
drawable->line_to(0.1, transfer_y(i));
}
drawable->move_to(0, 0);
drawable->end_path();
};
#include <stdio.h>
void Cartesian::set_x_ratio(float _x)
{
printf("x==%f\n", _x);
this->x_ratio = _x;
};
void Cartesian::set_y_ratio(float _y)
{
printf("y==%f\n", _y);
this->y_ratio = _y;
};
float Cartesian::transfer_x(float _x)
{
return _x * this->x_ratio;
};
float Cartesian::transfer_y(float _y)
{
return _y * this->y_ratio;
};
#endif // _CARTESIAN_CPP_