Rotating previously rotated text.

edited April 2018 in Library Questions

I am trying to rotate text to shift a it a few degrees clockwise to that it protrudes from the middle section of each arc. However, any time I use the rotate function it throws it way off. Any tips on what to use. I know it's the 'rotate()' function but I'm not getting it right.

Thanks in advance :)

    float delta = TWO_PI / childrenA.length; 
    float xPos = width/2+scaler/2*arrayDueTimes.get(i) * cos(i* delta); 
    float yPos = height/2+scaler/2*arrayDueTimes.get(i) * sin(i* delta);

    pushMatrix();
      translate(xPos, yPos);
      rotate(delta * i);
      fill(0);
      text(i + "--------------------", 0, 0); 
    popMatrix(); 

Answers

  • Please post a complete sketch that illustrates your problem. At the moment, you are expecting everyone to fill-in code to get a working sketch. ;)

  • Sorry, @Niels. Here's the full code. The code above is contains from the lines 208 to 217. Thank you :)

    import ddf.minim.*;
    import ddf.minim.effects.*;
    AudioPlayer[] mplayer;
    Minim minim;
    
    String[] songs={"train1.wav", "train2.wav"};
    int idx=0;
    
    
    import controlP5.*;
    import java.util.*;
    ControlP5 cp5;
    
    String urlStationInfo = "http://" + "api.irishrail.ie/realtime/realtime.asmx/getStationsFilterXML?StationText=";
    
    XML xmlA; 
    XML[] childrenA;
    
    XML xmlS; 
    XML[] childrenS;
    
    
    //String[] arrayStationIDs;
    //String[] arrayStationIDS = new String[childrenS.length]; // Array to store x-coordinates
    
    String[] storedID;
    String[] storedName;
    
    StringList arrayOrigin;
    StringList arrayDestination;
    FloatList arrayDueTimes;
    IntList arrayLateTimes;
    StringList arrayID;
    StringList stationID;
    
    int index;
    int angle=0; 
    int scaler = 8;
    
    Timer timer = new Timer(30000);
    
    
    //________________________________________________________________________________________________________________________________ 
    void setup() {
      size(800, 800);  
      minim = new Minim(this);
      mplayer=new AudioPlayer[2];
    
      for (int i=0; i<songs.length; i++) {
        mplayer[i] = minim.loadFile(songs[i]);
        mplayer[i].play();
        mplayer[i].pause();
      }
    
      cp5 = new ControlP5(this); 
    
      requestStationData();
      requestArrayData();  
      timer.start();
    
    
      for (int i = 0; i < childrenS.length; i++) {
        cp5.addScrollableList("dropdown")
           .setPosition(10, 10)
           .setSize(100, 200)
           .setBarHeight(20)
           .setItemHeight(20)
           .addItems(storedName)
           .setType(ScrollableList.LIST) // currently supported DROPDOWN and LIST
           ;
      }
    } 
    //________________________________________________________________________________________________________________________________ 
    void draw() {
      background(255,  193,  37);
    
      if (timer.isFinished()) {   // Every one second, make a new request.
        println("Timer is finished: Requesting values and restarting"); 
        requestStationData();
        requestArrayData();   
        timer.start();  // And restart the timer. 
      }
      drawArcs();
    }
    
    
    //________________________________________________________________________________________________________________________________ 
    void dropdown(int n) {
      /* request the selected item based on index n */
      println(n, cp5.get(ScrollableList.class, "dropdown").getItem(n));
      index = n;
      requestStationData();
      requestArrayData(); 
      timer.start();
    
      mplayer[idx].pause();
      idx=(int)random(songs.length);
      mplayer[idx].rewind();
      mplayer[idx].shiftGain(-80.0, 0, 5000);
      mplayer[idx].play();
      println("Randomly chosen sample is "+idx);  
    
    
      CColor c = new CColor();
      c.setBackground(color(255,0,0));
      cp5.get(ScrollableList.class, "dropdown").getItem(n).put("color", c); 
    }
    
    
    
    
    
    //________________________________________________________________________________________________________________________________ 
    void requestStationData() {   
      xmlS = loadXML(urlStationInfo);  
      childrenS = xmlS.getChildren("objStationFilter");  
    
      storedID = new String[childrenS.length]; // Array to store x-coordinates
      storedName = new String[childrenS.length]; // Array to store x-coordinates
      stationID = new StringList();
    
    
      for (int i = 0; i < childrenS.length; i++) {
        XML sIDElement = childrenS[i].getChild("StationCode"); 
        String sID = sIDElement.getContent();  
        storedID[i] = sID;
        stationID.append(sID);   
    
        XML sNameElement = childrenS[i].getChild("StationDesc"); 
        String sName = sNameElement.getContent();  
        storedName[i] = sName;
        //stationName.append(sName);  
    
    
      }   
    } 
    
    
    //________________________________________________________________________________________________________________________________ 
    void requestArrayData() {   
      arrayID = new StringList();
      arrayOrigin = new StringList();
      arrayDestination = new StringList();  
      arrayDueTimes = new FloatList();
      arrayLateTimes = new IntList();
    
      //xmlA = loadXML("stationData.xml");
      for (int i = 0; i < childrenS.length; i++) {    
        //String myString = stationID.get(i);
        String myString = stationID.get(index);
    
        //xmlA = loadXML("stationData.xml");
        xmlA = loadXML("http://api.irishrail.ie/realtime/realtime.asmx/getStationDataByCodeXML?StationCode=" + myString);  
    
        childrenA = xmlA.getChildren("objStationData");  
        //println(childrenA.length);
    
        for (int j = 0; j < childrenA.length; j++) {   
    
          XML originElement = childrenA[j].getChild("Origin");
          String origin = originElement.getContent();      
          arrayOrigin.append(origin);
    
          XML destinationElement = childrenA[j].getChild("Destination");
          String destination = destinationElement.getContent();   
          arrayDestination.append(destination);
    
          XML dueElement = childrenA[j].getChild("Duein");
          int due = dueElement.getIntContent();     
          arrayDueTimes.append(due);
    
          XML lateElement = childrenA[j].getChild("Late");
          int late = lateElement.getIntContent();     
          arrayLateTimes.append(late);
    
          //println(" Station: " + fullName + ". Next Train: " + origin + " to " + destination + " is due in " + due + "minutes.");
        } 
      } 
    }
    
    //________________________________________________________________________________________________________________________________ 
    
    void drawArcs(){  
      float startAngle = 0;  
      float lastAngle = 0;
      float radian = 360.0/childrenA.length;
    
      pushMatrix();
      translate(width/2, height/2);
    
      for (int i = 0; i < childrenA.length; i++) {   
    
        // This collection of arcs is printed to screen. Each arc will protude by the pixel amount in minutes that its corresponding train is overdue
        fill(255,0,0);
        stroke(0);
        strokeWeight(0);
        arc(0, 0, scaler*abs(arrayDueTimes.get(i))+arrayLateTimes.get(i), scaler*abs(arrayDueTimes.get(i))+arrayLateTimes.get(i), lastAngle, lastAngle+radians(radian));
    
    
        fill(0+i*255/childrenA.length);
        stroke(0);
        strokeWeight(0);
        arc(0, 0, scaler*(arrayDueTimes.get(i)), scaler*(arrayDueTimes.get(i)), lastAngle, lastAngle+radians(radian));
        lastAngle += radians(radian);
        startAngle = startAngle + lastAngle;
    
    
        float delta = TWO_PI / childrenA.length; 
        float xPos = 0+scaler/2*arrayDueTimes.get(i) * cos(i* delta); 
        float yPos = 0+scaler/2*arrayDueTimes.get(i) * sin(i* delta);
    
        pushMatrix();
          translate(xPos, yPos);
          rotate(delta * i);
          fill(0);
          text(i + "--------------------", 0, 0); 
        popMatrix(); 
      }//for 
      popMatrix(); 
    }
    
    //________________________________________________________________________________________________________________________________ 
    
    class Timer {
      int savedTime;
      boolean running = false;
      int totalTime;
    
      Timer(int tempTotalTime) {
        totalTime = tempTotalTime;
      }
    
      void start() {
        running = true;
        savedTime = millis();
      }
    
      boolean isFinished() {
        int passedTime = millis() - savedTime;
        if (running && passedTime > totalTime) {
          running = false;
              return true;
            } else {
              return false;
            }
      }
    }
    
  • edited April 2018

    Yikes. No. That's not what he meant. Post a working sketch that demonstrates only your issue. Remove any code from it that isn't related. We aren't going to trudge through 250 lines of code to help you with rotating text.

    Post a complete, working sketch that just shows your text. Also post maybe a mock-up image of what you wish your rotated text looked like.

  • Roger that. Sorry I was sleep deprived when I posted. Will condense and re-post.

  • OP stated the line numbers though.....

    Line 189 posing a problem?

  • Here we go, apologies once again.

    I basically want to rotate it so that the writing just moves clockwise! around the circle so that it lies in the middle of each segment. So in the screen shot, the red lines are where I want the text to be. I also want to make it so that the text will remain in the middle of each segment no matter how many arcs I add to that circle. (6 are added in the code below but this number will be changing in my larger script)

    Screen Shot 2018-04-24 at 12.43.08

    Below is only the relevant code. I hope I mad more sense this time.

    void setup() {
      size(800, 800);  
    }
    
    //________________________________________________________________________________________________________________________________ 
    void draw() {
      background(255,  193,  37);
      drawArcs();
    }
    
    //________________________________________________________________________________________________________________________________ 
    void drawArcs(){  
      float startAngle = 0;  
      float lastAngle = 0;
    
      pushMatrix();
      translate(width/2, height/2);
    
      for (int i = 0; i < 6; i++) {   
    
        fill(0+i*255/6);
        stroke(0);
        strokeWeight(0);
        arc(0, 0, 300, 300, lastAngle, lastAngle+radians(360.0/6));
        lastAngle += radians(360.0/6);
        startAngle = startAngle + lastAngle;
    
        float delta = TWO_PI / 6; 
        float xPos = 200 * cos(i* delta); 
        float yPos = 200 * sin(i* delta);
    
        pushMatrix();
          translate(xPos, yPos);
          rotate(delta * i);
          fill(0);
          text(i + "--------------------", 0, 0); 
        popMatrix(); 
      }//for 
      popMatrix(); 
    }
    
  •   // translate(xPos, yPos);   //CHANGED
      rotate(delta * i);
      fill(0);
      text(i + "--------------------", 200, 0);  //CHANGED
    

    Full working example below.

    Kf

    float dyna;   //dynamic angle
    
    void setup() {
      size(800, 800,P2D);
    }
    
    //________________________________________________________________________________________________________________________________
    void draw() {
      background(255, 193, 37);
      drawArcs();
    }
    
    //________________________________________________________________________________________________________________________________
    void drawArcs() { 
      float startAngle = 0; 
      float lastAngle = 0;
    
      pushMatrix();
      translate(width/2, height/2);
    
      for (int i = 0; i < 6; i++) {  
    
        fill(0+i*255/6);
        stroke(0);
        strokeWeight(0);
        arc(0, 0, 300, 300, lastAngle, lastAngle+radians(360.0/6));
        lastAngle += radians(360.0/6);
        startAngle = startAngle + lastAngle;
    
        float delta = TWO_PI / 6;
        float xPos = 200 * cos(i* delta);
        float yPos = 200 * sin(i* delta);
    
        pushMatrix();
        rotate(delta * i+dyna);
        //translate(xPos, yPos);
    
        fill(0);
        text(i + "--------------------", 200,0);
        popMatrix();
      }//for
      popMatrix();
    
      if (frameCount%5==0) {
        dyna+=0.01;
        if (dyna>0.5) dyna=0;
      }
    }
    
  • Thank you, @kfrajer, sir :) Works a treat within my original code too. Shall I post the full (large) working code?

    Screen Shot 2018-04-24 at 21.48.30

  • That looks great. If you want to share your code, yes please do. With this image, people would be interested to see and learn from your approach.

    Kf

  • This just occurred to me. This is due for an assignment shortly, so I don't want to risk plagiarising myself if the lecturer find this code online. (I know I already posted some of the code)

    I can post a link to it separately in a google doc and post the code as text to this thread after it has been corrected?

    Here's the code on my google drive: https://drive.google.com/open?id=1IAIHDlMw6DEDidqNPuzuCm97KhvwzCue

  • Thanks for sharing and good luck with the assignment. ;)

Sign In or Register to comment.