制作24小时制。 24小时是钟表将从零小时计数到23小时。但钟面只有小时的AM编号或任何时候显示的小时的PM编号。 AM编号从0到11. PM从12到23 我需要使用drawNum代码,我无法使其工作, 这是我到目前为止的代码......
float x[], y[];
float diam;
color c[];
int n; //number of balls
void setup()
{
size (500, 500);
colorMode(HSB);
background(0);
frameRate(1);
x = new float [n];
y = new float [n];
}
void draw()
{
float h, m, s;
float radius;
float cx, cy;
float clockface;
float hoursRadius, minutesRadius, hoursTick, secondsRadius, minutesTick;
radius = min(height/2.0, width/2.0);
cx = width/2.0;
cy = height/2.0;
clockface = radius * 0.9;
hoursRadius = radius * 0.5;
minutesRadius = radius * 0.65;
secondsRadius = radius * 0.72;
hoursTick = radius * 0.04;
minutesTick = hoursTick * 0.5;
// get time
s = second();
m = minute();
h = hour()%12 + m/60.0;
//draw clock face
fill(40);
noStroke();
ellipseMode(RADIUS);
ellipse(cx, cy, clockface, clockface);
//
drawHand(cx, cy, s*6.0, secondsRadius, 2);
drawHand(cx, cy, m*6.0, minutesRadius, 3);
drawHand(cx, cy, h*30.0, hoursRadius, 5);
for ( int i=0; i<60; i++) {
if (i%5==0) {
drawNum(cx, cy, i*6, secondsRadius, 10, 0);
} else {
drawNum(cx, cy, i*6, secondsRadius, 5, 23);
}
}
// draw the ticks
for ( int i=0; i<60; i++) {
if (i%5==0) {
drawTick(cx, cy, i*6, secondsRadius, 10);
} else {
drawTick(cx, cy, i*6, secondsRadius, 5);
}
}
}
void drawTick( float x, float y,
float angle, float len,
float weight) {
fill(angleToColor(angle));
noStroke();
rectMode(CENTER);
rect(x + cos(radians(angle-90))*len,
y + sin(radians(angle-90))*len,
weight, weight) ;
}
void drawHand( float x, float y,
float angle, float len,
float weight) {
strokeWeight(weight);
stroke(angleToColor(angle));
line(x, y,
x + cos(radians(angle-90))*len,
y + sin(radians(angle-90))*len);
}
color angleToColor(float angle) {
return color(map(angle, 0, 360, 0, 255), 255, 255);
}
void drawNum(float x,float y,
float angle, float len,
float tsize, int num) {
float i;
textSize(36);
fill(0);
noStroke();
for (num = 0; num < 12; num++) {
int hoursNum = (x + cos(radians(angle-90))*len,
y + sin(radians(angle-90))*len,
num, num)) ;;
}
}
答案 0 :(得分:-1)
您需要将数字转换为字符串(文本)以显示它。这是通过函数str(arg)
完成的,它返回类型String
的值。参数arg
可以是int,float,char,byte或boolean类型。
绘制文字的功能是text( s, x, y )
,其中s
是要显示的字符串,x
和y
是位置。
修改强>
hour()
函数返回24小时制的小时数,从0-23而不是1-24开始计算。右键单击Processing窗口中的函数,然后选择“Find in Reference”以查看详细信息。代码hour()%12
将小时值取模12,这意味着当值达到12时,它将循环回零,而值12-23则报告为0-11。