How do I convert a decimal value of a degree?

edited November 2013 in How To...

I am trying to take the decimal value of adjacent/hypotenuse and convert it to a degree i.e.

 [cos^-1(0.1827)] = 79.5

How do I code cos raised to -1 etc...

Answers

  • edited November 2013
    void setup() {
      size(530,600);
      background(255);
    }
    
    void draw() {
      background(255);
      stroke(0);
      strokeWeight(4);
      noFill();
      if(mouseX<200) {
        //triangle(0, 0, 150, 0, 150, mouseY);
        stroke(255,0,0);
        line(150,0,150,mouseY);
        stroke(0,0,255);
        line(0,0,150,mouseY);
        stroke(0,100,0);
        line(0,0,150,0);
        drawLeftReference(mouseY);
        text("A",135,mouseY+10);
        text("B",20,15);
      } else if(mouseX>200) {
        //triangle(350,0,500,0,350,mouseY);
        stroke(0,0,255);
        line(500,0,350,mouseY);
        stroke(255,0,0);
        line(350,0,350,mouseY);
        stroke(0,100,0);
        line(350,0,500,0);
        drawRightReference(mouseY);
        text("A",355,mouseY+10);
        text("B",505,15);
      }
    }
    
    void drawLeftReference(float yValue) {
      stroke(0);
      strokeWeight(1);
      line(135,0,135,15);
      line(135,15,150,15);
      text("Triangle Reference", 165,15);
      fill(0,0,255);
      text("Hypotenuse = " + findHypot(mouseY), 165,35);
      fill(255,0,0);
      text("Adjacent = " + mouseY, 165,55);
      fill(0,100,0);
      text("Opposite = " + 150, 165,75);
      fill(0);
      text("Angle A = " + exp(-1*cos(yValue/findHypot(mouseY))), 165,95);
      text("Angle B = " + 150/findHypot(mouseY), 165,115);
    }
    void drawRightReference(float yValue) {
      stroke(0);
      strokeWeight(1);
      line(350,15,365,15);
      line(365,0,365,15);
      text("Triangle Reference", 165,15);
      fill(0,0,255);
      text("Hypotenuse = " + findHypot(mouseY), 165,35);
      fill(255,0,0);
      text("Adjacent = " + mouseY, 165,55);
      fill(0,100,0);
      text("Opposite = " + 150, 165,75);
      fill(0);
      text("Angle A = " + yValue/findHypot(mouseY), 165,95);
      text("Angle B = " + 150/findHypot(mouseY), 165,115);
    }
    
    float findHypot(float yVal) {
      yVal = sqrt((150*150)+(yVal*yVal));
      return yVal;
    }
    
  • edited October 2013

    Please format your code before pasting. You can do that with Command+t.

    If I understand your question, you have an angle that you want in degrees. The trig functions use radians and can be converted with degrees(): http://processing.org/reference/degrees_.html

  • edited November 2013

    no, i think he just wants the arccos function name:

    http://processing.org/reference/acos_.html

    see also http://processing.org/reference/asin_.html and http://processing.org/reference/atan2_.html

    (atan2() is recommended over atan())

  • edited November 2013

    I tried degrees() and its close, must have something off...

    void setup() {
      size(530,600);
      background(255);
    }
    
    void draw() {
      background(255);
      stroke(0);
      strokeWeight(4);
      noFill();
      if(mouseX<200) {
        //triangle(0, 0, 150, 0, 150, mouseY);
        stroke(255,0,0);
        line(150,0,150,mouseY);
        stroke(0,0,255);
        line(0,0,150,mouseY;)
        stroke(0,100,0);
        line(0,0,150,0);
        drawLeftReference(mouseY);
        text("A",135,mouseY+10);
        text("B",20,15);
      } else if(mouseX>200) {
        //triangle(350,0,500,0,350,mouseY);
        stroke(0,0,255);
        line(500,0,350,mouseY);
        stroke(255,0,0);
        line(350,0,350,mouseY);
        stroke(0,100,0);
        line(350,0,500,0);
        drawRightReference(mouseY);
        text("A",355,mouseY+10);
        text("B",505,15);
      }
    }
    
    void drawLeftReference(float yValue) {
      stroke(0);
      strokeWeight(1);
      line(135,0,135,15);
      line(135,15,150,15);
      text("Triangle Reference", 165,15);
      fill(0,0,255);
      text("Hypotenuse = " + findHypot(mouseY), 165,35);
      fill(255,0,0);
      text("Adjacent = " + mouseY, 165,55);
      fill(0,100,0);
      text("Opposite = " + 150, 165,75);
      fill(0);
      text("Angle A = " + degrees(150/findHypot(mouseY)), 165,95);
      text("Angle B = " + degrees(yValue/findHypot(mouseY)), 165,115);
    
    }
    void drawRightReference(float yValue) {
      stroke(0);
      strokeWeight(1);
      line(350,15,365,15);
      line(365,0,365,15);
      text("Triangle Reference", 165,15);
      fill(0,0,255);
      text("Hypotenuse = " + findHypot(mouseY), 165,35);
      fill(255,0,0);
      text("Adjacent = " + mouseY, 165,55);
      fill(0,100,0);
      text("Opposite = " + 150, 165,75);
      fill(0);
      text("Angle A = " + degrees(150/findHypot(mouseY)), 165,95);
      text("Angle B = " + degrees(yValue/findHypot(mouseY)), 165,115);
    
    }
    
    float findHypot(float yVal) {
      yVal = sqrt((150*150)+(yVal*yVal));
      return yVal;
    }
    
  • degrees just converts from radians to degrees, a simple multiplication.

    you want acos() etc, which are what you're calling cos^-1

    http://en.wikipedia.org/wiki/Inverse_trigonometric_function

  • (^ answer will be in radians)

  • Answer ✓

    This might make it clearer

    float angRads = acos(0.1827);
    float angDegs = degrees(angRads);
    println(angDegs);
    

    will display 79.47293

  • Thanks... thats it!

Sign In or Register to comment.