Subversion Repositories DIN Is Noise

Rev

Rev 1857 | Rev 2097 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
964 jag 1
/*
2
* circler.cc
2009 jag 3
* DIN Is Noise is copyright (c) 2006-2023 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);
67
  sp.radius.set ("Radius", 0.01f, 0.0f, MILLION, this, 0);
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
 
1542 jag 84
  radius = sp.radius.f_value;
1544 jag 85
  startangle = sp.startangle.f_value;
1542 jag 86
  endangle = sp.endangle.f_value;
964 jag 87
 
1542 jag 88
  num_points = sp.num_points.f_value;
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) {
1544 jag 121
  if (&f == &sp.startangle.f_value) {
122
    startangle = float(sp.startangle.f_value);
1542 jag 123
  }
124
  plugin::changed (f);
125
}
126
 
964 jag 127
/*void circler::write () {
128
  extern string user_data_dir;
129
  string fname (user_data_dir + "circler.pts");
130
  ofstream fout (fname.c_str(), ios::out);
131
  if (fout) {
132
    fout << "center 0.5 0" << endl;
133
    fout << "num_points " << num_points << endl;
134
    for (int i = 0, n = points.size () - 1; i < n; ++i) {
135
      point<float>& pti = points[i];
136
      fout << pti.x << ' ' << pti.y << endl;
137
    }
138
  }
139
}*/