Rev 2107 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
964 | jag | 1 | /* |
2 | * circler.cc |
||
2302 | jag | 3 | * DIN Is Noise is copyright (c) 2006-2025 Jagannathan Sampath |
1713 | jag | 4 | * DIN Is Noise is released under GNU Public License 2.0 |
1479 | jag | 5 | * For more information, please visit https://dinisnoise.org/ |
964 | jag | 6 | */ |
7 | |||
8 | #include "circler.h" |
||
9 | #include "vector2d.h" |
||
10 | #include "ui_list.h" |
||
11 | #include "console.h" |
||
12 | #include <vector> |
||
13 | #include <fstream> |
||
14 | using namespace std; |
||
15 | extern ofstream dlog; |
||
16 | extern ui_list uis; |
||
17 | extern string user_data_dir; |
||
18 | extern const float PI_BY_180; |
||
19 | extern const float TWO_PI; |
||
20 | extern curve_library sin_lib, cos_lib; |
||
21 | |||
22 | circler::circler () : scr (this, "circler.scr", "circler_sin.crv", "circler_cos.crv", "circler_radius.crv", "circler_sin.ed", "circler_cos.ed", "circler_radius.ed", 1) { |
||
23 | name = "Circler"; |
||
24 | center.x = 0.5; |
||
25 | center.y = 0; |
||
26 | load_params (); |
||
27 | } |
||
28 | |||
29 | circler::~circler () { |
||
30 | widget_save ("d_circler", ctrls); |
||
31 | save_params (); |
||
32 | } |
||
33 | |||
34 | void circler::load_params () { |
||
35 | string ignore; |
||
36 | ifstream f (make_fname().c_str(), ios::in); |
||
1560 | jag | 37 | f >> ignore >> radius >> ignore >> startangle >> ignore >> endangle >> ignore >> num_points; |
964 | jag | 38 | } |
39 | |||
40 | void circler::save_params () { |
||
41 | ofstream f (make_fname().c_str(), ios::out); |
||
42 | f << "radius " << radius << endl; |
||
1544 | jag | 43 | f << "startangle " << startangle << endl; |
1542 | jag | 44 | f << "endangle " << endangle << endl; |
964 | jag | 45 | f << "num_points " << num_points << endl; |
46 | } |
||
47 | |||
48 | void circler::setup () { |
||
49 | |||
50 | plugin::setup (); |
||
51 | |||
1560 | jag | 52 | widget* _ctrls [] = {&sp.radius, &sp.startangle, &sp.endangle, &sp.num_points, &scr}; |
53 | for (int i = 0; i < 5; ++i) ctrls.push_back (_ctrls[i]); |
||
1542 | jag | 54 | |
1560 | jag | 55 | /*for (int i = 0; i < 7; ++i) { |
1542 | jag | 56 | ctrls[i]->set_moveable(1); |
57 | }*/ |
||
1550 | jag | 58 | |
964 | jag | 59 | num_ctrls = ctrls.size (); |
60 | |||
1559 | jag | 61 | sp.startangle.set ("Start angle", 1.0f, -MILLION, MILLION, this, 0); |
1544 | jag | 62 | sp.startangle.set_value (startangle); |
1559 | jag | 63 | sp.endangle.set ("End angle", 1.0f, -MILLION, MILLION, this, 0); |
1542 | jag | 64 | sp.endangle.set_value (endangle); |
65 | sp.num_points.set ("Points", 1, 1, MILLION, this, 0); |
||
66 | sp.num_points.set_value (num_points); |
||
2097 | jag | 67 | sp.radius.set ("Radius", 0.01f, -MILLION, MILLION, this, 0); |
1542 | jag | 68 | sp.radius.set_value (1.0f); |
964 | jag | 69 | |
70 | widget_load ("d_circler", ctrls); |
||
71 | |||
72 | scr.setup (); |
||
73 | scr.set_pos (cb_auto_apply.posx, cb_auto_apply.posy); |
||
74 | sin_cos_radius_optioned (); |
||
75 | |||
76 | } |
||
77 | |||
78 | void circler::render () { |
||
79 | |||
80 | funktion& f_radius = *scr.pf_radius; |
||
81 | funktion& f_sin = *scr.pf_sin; |
||
82 | funktion& f_cos = *scr.pf_cos; |
||
83 | |||
2107 | jag | 84 | radius = sp.radius.value; |
85 | startangle = sp.startangle.value; |
||
86 | endangle = sp.endangle.value; |
||
964 | jag | 87 | |
2107 | jag | 88 | num_points = sp.num_points.value; |
1542 | jag | 89 | |
1544 | jag | 90 | theta = startangle * PI_BY_180; |
91 | dtheta = (endangle - startangle) / num_points * PI_BY_180; |
||
964 | jag | 92 | |
1542 | jag | 93 | int j = num_points + 1; |
999 | jag | 94 | points.resize (j); |
1542 | jag | 95 | |
999 | jag | 96 | for (int i = 0; i < j; ++i) { |
978 | jag | 97 | point<float>& p = points[i]; |
98 | p.x = center.x + f_radius (theta) * radius * f_cos (theta); |
||
99 | p.y = center.y + f_radius (theta) * radius * f_sin (theta); |
||
964 | jag | 100 | theta += dtheta; |
1546 | jag | 101 | if (theta > TWO_PI) theta -= TWO_PI; // esp for custom periodics behind f_sin, f_cos & f_radius too |
964 | jag | 102 | } |
103 | |||
978 | jag | 104 | gen_pts (); |
105 | |||
964 | jag | 106 | ss.str(""); |
107 | ss << "circle_" << num_points; |
||
108 | |||
978 | jag | 109 | |
964 | jag | 110 | } |
111 | |||
1542 | jag | 112 | void circler::sin_cos_radius_edited () { |
113 | do_render (); |
||
114 | } |
||
115 | |||
116 | void circler::sin_cos_radius_optioned () { |
||
117 | do_render (); |
||
118 | } |
||
119 | |||
120 | void circler::changed (field& f) { |
||
2107 | jag | 121 | if (&f == &sp.startangle.f_value) startangle = sp.startangle.value; |
1542 | jag | 122 | plugin::changed (f); |
123 | } |
||
124 | |||
964 | jag | 125 | /*void circler::write () { |
126 | extern string user_data_dir; |
||
127 | string fname (user_data_dir + "circler.pts"); |
||
128 | ofstream fout (fname.c_str(), ios::out); |
||
129 | if (fout) { |
||
130 | fout << "center 0.5 0" << endl; |
||
131 | fout << "num_points " << num_points << endl; |
||
132 | for (int i = 0, n = points.size () - 1; i < n; ++i) { |
||
133 | point<float>& pti = points[i]; |
||
134 | fout << pti.x << ' ' << pti.y << endl; |
||
135 | } |
||
136 | } |
||
137 | }*/ |