reducing redundancy from functions
in
Programming Questions
•
1 years ago
Hi. I've got some code and I'd like to ask for an idea on how to reduce some of the redundancy in the functions.
The code:
PFont TimesNewRoman;
int n; //number of lines
int s = 1;
void setup() {
size(400, 400);
background(255);
frameRate(1);
noLoop();
TimesNewRoman = loadFont("TimesNewRomanPS-ItalicMT-36.vlw");
textFont(TimesNewRoman, 32);
}
void draw() {
n = int(random(1, 7));
fill(200);
background(255);
ellipse(width/2, height/2, 100, 100);
translate(width/2, height/2); //move origin to center
drawLines(n);
drawText(n, "element");
}
void drawLines(int nLines) {
int n = nLines;
int c = 360;
int r = 125;
int i = 0;
for(i=0; i<n; i++){
float a = c/n;
float b = a*i;
float d = 270;
float xPos = cos(radians(d+b))*r;
float yPos = sin(radians(d+b))*r;
line(0, 0, xPos/1.5, yPos/1.5);
}
}
void drawText(int nLines, String word) {
int n = nLines;
int c = 360;
int r = 125;
int i = 0;
for(i=0; i<n; i++){
float a = c/n; // 360/3=120
float b = a*i; // 120*0, 120*1, 120*2,...
float d = 270; // 90deg
float xPos = cos(radians(d+b))*r;
float yPos = sin(radians(d+b))*r;
fill(0);
textAlign(CENTER);
text(word, xPos, yPos);
}
}
void mousePressed()
{
redraw();
}
My thought was to declare global variables for the X,Y positions to be used in the drawLines and drawText functions, then put the geometry into a separate single function. But how would I return multiple pieces of data from that?
Thanks in advance.
The code:
PFont TimesNewRoman;
int n; //number of lines
int s = 1;
void setup() {
size(400, 400);
background(255);
frameRate(1);
noLoop();
TimesNewRoman = loadFont("TimesNewRomanPS-ItalicMT-36.vlw");
textFont(TimesNewRoman, 32);
}
void draw() {
n = int(random(1, 7));
fill(200);
background(255);
ellipse(width/2, height/2, 100, 100);
translate(width/2, height/2); //move origin to center
drawLines(n);
drawText(n, "element");
}
void drawLines(int nLines) {
int n = nLines;
int c = 360;
int r = 125;
int i = 0;
for(i=0; i<n; i++){
float a = c/n;
float b = a*i;
float d = 270;
float xPos = cos(radians(d+b))*r;
float yPos = sin(radians(d+b))*r;
line(0, 0, xPos/1.5, yPos/1.5);
}
}
void drawText(int nLines, String word) {
int n = nLines;
int c = 360;
int r = 125;
int i = 0;
for(i=0; i<n; i++){
float a = c/n; // 360/3=120
float b = a*i; // 120*0, 120*1, 120*2,...
float d = 270; // 90deg
float xPos = cos(radians(d+b))*r;
float yPos = sin(radians(d+b))*r;
fill(0);
textAlign(CENTER);
text(word, xPos, yPos);
}
}
void mousePressed()
{
redraw();
}
My thought was to declare global variables for the X,Y positions to be used in the drawLines and drawText functions, then put the geometry into a separate single function. But how would I return multiple pieces of data from that?
Thanks in advance.
1