-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sun.pde
176 lines (148 loc) · 4.11 KB
/
Sun.pde
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
This class is relatively simple, should just be drawing the sun in a position relative to the current time. The sun rises from the
east to the west, so east earlier in the morning, west later in the afternoon.
*/
class Sun {
//Position variables
float xPos;
float yPos;
//Sun size
float sunRadius = 100;
//Sun position limiters
int sunBaseline;
int sunRaiseHeight;
//Angle to use for calculating height.
float sunAngle;
//trigger drawing sun
boolean sunActive;
boolean autoSunMovement;
//time var
//It's safe to use these to refer to the current time as these are updated even in auto mode.
int manualHour;
int manualMinute;
//PGraphics
PGraphics sunPG;
PGraphics outline;
Sun(int inpBaseline, int inpRaiseHeight) {
sunBaseline = inpBaseline;
sunRaiseHeight = inpRaiseHeight;
autoSunMovement = true;
pushMatrix();
sunPG = createGraphics((int)sunRadius * 2, (int)sunRadius * 2, P2D);
sunPG.beginDraw();
sunPG.stroke(255);
sunPG.strokeWeight(4);
sunPG.fill(255, 255, 0);
sunPG.ellipse(sunRadius, sunRadius, sunRadius, sunRadius);
sunPG.filter(BLUR, 2);
sunPG.endDraw();
popMatrix();
pushMatrix();
outline = createGraphics((int)sunRadius*2, (int)sunRadius * 2, P2D);
outline.beginDraw();
outline.stroke(255);
outline.strokeWeight(4);
outline.noFill();
outline.ellipse(sunRadius, sunRadius, sunRadius, sunRadius);
outline.endDraw();
outline.filter(BLUR, 1);
popMatrix();
}
void drawSun() {
if (autoSunMovement) {
autoSetSunPosition();
} else {
manualSetSunPosition();
}
if (sunActive) {
pushMatrix();
tint(255, 255);
imageMode(CENTER);
image(outline, xPos, yPos);
image(sunPG, xPos, yPos);
popMatrix();
}
}
//Calculate and set the position for the sun
void autoSetSunPosition() {
int currentMinute = calculateMinutes(hour(), minute());
manualHour = hour();
manualMinute = minute();
if (currentMinute != -1) {
sunActive = true;
sunAngle = currentMinute / 4;
yPos = sunBaseline - sin(radians(sunAngle)) * sunRaiseHeight;
xPos = width * ((float)currentMinute / 780);
// println("xpos: " + xPos);
// println("ypos: " + yPos);
// println("currentMinute: " + currentMinute);
return;
}
sunActive = false;
}
void manualSetSunPosition() {
int currentMinute = calculateMinutes(manualHour, manualMinute);
if (currentMinute != -1) {
sunActive = true;
sunAngle = currentMinute / 4;
yPos = sunBaseline - sin(radians(sunAngle)) * sunRaiseHeight;
xPos = width * ((float)currentMinute / 720) - 30;
// println("xpos: " + xPos);
// println("ypos: " + yPos);
// println("currentMinute: " + currentMinute);
return;
}
sunActive = false;
}
//Calculate minutes from 6AM to 6PM, return -1 if outside of those times.<---
int calculateMinutes(int inpHour, int inpMinute) {
int totalMinutes = 0;
if ( (inpHour >= 5) && (inpHour <= 19) ) {
totalMinutes += (inpHour - 6) * 60;
totalMinutes += inpMinute;
return totalMinutes;
}
return -1;
}
//Changes whether if the sun should be drawn at the right position in the sky.
void setAutoSun() {
autoSunMovement = !autoSunMovement;
println("current Sun set to: " + autoSunMovement);
}
boolean isAutoSun(){
return autoSunMovement;
}
void sunForward() {
manualMinute++;
if (manualMinute >= 60) {
manualHour++;
manualMinute = 0;
}
if (manualHour >= 24) {
manualHour = 0;
}
}
void sunBack() {
manualMinute--;
if (manualMinute < 0) {
manualHour--;
manualMinute = 59;
}
if (manualHour < 0) {
manualHour = 23;
manualMinute = 59;
}
}
String currentTime() {
if (manualMinute >= 0 && manualMinute < 10) {
return manualHour + ":0" + manualMinute;
}
return manualHour + ":" + manualMinute;
}
int getCurrentHour() {
return manualHour;
}
int getCurrentMinute() {
return manualMinute;
}
}