Making Clock with Pendulum

This is for an assignment in my Computational Thinking class, and I am _not _ the best with Processing, so if you could try to just hint me which lines I will need to edit to accomplish certain things (not what to actually do) and/or other lines in the code that I can play with and take example from to perform other actions.

Currently I am trying to make the pendulum, so I know I need to use a draw function to draw a line oriented at the center of the frame, but I'm not sure how or where. Our teacher gave us a line of code to start us out for the pendulum but I don't know what to do with it. I apologize for the commentary in the code and, again, when it comes to processing, I am not the brightest pixel in the screen.

void setup()
{
  size(1000, 2000);
  colorMode(HSB);
  background(0);
  frameRate(30);
  surface.setResizable(false); //I can probably take this out can't i huh

}


void draw()
{
  float h, m, s;
  float radius;
  float cx, cy;

  float clockface;
  float hoursRadius, minutesRadius, secondsRadius;
  float minuteDotSize, hourDotSize;

  String months[] = {"XXX", "Jan", "Feb", "Mar", "Apr"};
  String days[] = {};

  radius = min(height, width)/2.0;
  cx = width/2.0;
  cy = height/4.0;

  clockface = radius*0.9;
  hoursRadius = radius*0.50;
  minutesRadius = radius*0.65;
  secondsRadius = radius*0.72;
  minuteDotSize = radius*0.015;
  hourDotSize = radius*0.03;


  s = second();
  m = minute();
  h = hour()%12; //Will handle this last because it's kiiinda difficult to know if it broke the function or not


  fill(128);
  noStroke();
  ellipseMode(RADIUS);
  ellipse(cx, cy, clockface, clockface);

  textSize(150);
  fill(0, 255, 255);
  textAlign(CENTER, CENTER); //now how do I get it below the clockface
  text(months[1+3], cx, cy); //HOW TO DAYs?


  for (float pos=0; pos<60; pos+=1) {
    if (pos%5==0) {
      drawTick(cx, cy, pos*6, secondsRadius, hourDotSize);
    } else {
      drawTick(cx, cy, pos*6, secondsRadius, minuteDotSize);
    }
  }


  drawHand(cx, cy, s*6, secondsRadius, 1);
  drawHand(cx, cy, m*6, minutesRadius, 2);
  drawHand(cx, cy, h*30, hoursRadius, 4);
}


float handPos(float angle)
{
  return radians(angle-90);
}


color colorPos(float angle)
{
  return color(map(angle, 0, 360, 0, 255), 255, 255);
}


void drawHand(float cx, float cy, float angle, float len, float weight)
{
  stroke(colorPos(angle));
  strokeWeight(weight);
  line(cx, cy, cx + cos(handPos(angle))*len, cy + sin(handPos(angle))*len);
}


void drawTick(float cx, float cy, float angle, float len, float size)
{
  fill(colorPos(angle));
  noStroke();
  rectMode(CENTER);
  rect(cx + cos(handPos(angle))*len, cy + sin(handPos(angle))*len, size, size);
}

// TO DO AND OR KEEP TESTING AND MESSING UP

void drawNum(float cx, float cy, float angle, float len, float weight, int num)
//should i be referring to the tick mark code i feel like I should be smart and do that

{
  int maxh; //i don't even know if this is right and I already feel like a genius for trying
  int n;

}

void drawPend(float cx, float cy, float angle, float len, float size, float weight)

{
//clear() for the clear every frame requirement right?

//pendRadius = radius*1.8;
//pendSize = hourDotSize*8.0;
//pendWeight = 3;

}

// told me I had duplicate draw hand, gotta go put more commands in the one draw hand area then?
// why is coding giving me an existential crisis

Answers

  • The code you have posted runs without issue. It also draws something, and appears to even tell the time (almost)! What is not clear, however, is what you are trying to add/change to it. You want a pendulum? Drawn where? Behind the clock, below it? What does the pendulum look like? Does it swing back and forth? How fast? How far out does it swing?

    I would honestly sugguest you put this body of code on-hold for right now, and instead start with a blank sketch. Then add a pendulum to it - and just a pendulum. And once you have that working, and only then, try adding it to this sketch.

  • Oh, and a pendulum has a formula, one with a bit of math.

  • edited May 2017

    @TurtleTeaParty -- re:

    I need to use a draw function to draw a line oriented at the center of the frame

    To move the frame of reference during draw() so that (0,0) is at the center of the sketch frame:

    translate(width/2,height/2);
    

    Now, draw a line straight down:

    line(0,0,0,myLength);
    

    But wait -- what if you wanted that straight line to be straight down... in a rotated direction?

    translate(width/2,height/2);
    rotate(myRadians);
    line(0,0,0,myLength);
    

    Since you want to play with the code and learn basic concepts around how to set up line orientations, I'd recommend reading this tutorial:

    Then consider checking out some of the many ways that recent forum-goers have tackled pendulum problems:

Sign In or Register to comment.