Search code examples
javaprocessingtrigonometryvector-graphicscurve

Processing: Arc code only creates circles. How to identify start and stop values correctly


I need to create an "arc" in this code. I've been working off some other code which successfully creates a circle, but I cannot understand how to implement the start and stop values correctly.

Essentially the code currently creates a circle still, and I'm not sure what to do.

This is part of a larger file, but I didn't think the rest was relevant. Let me know if I should add the rest.

class UIArc{
  float a, b, c, d, start, stop;
  public UIArc(float a, float b, float c, float d, float start, float stop){
    setArc(a, b, c, d, start, stop);
  }
  public UIArc(PVector p1, PVector p2){
    setArc(p1.x, p1.y, p2.x, p2.y, 90, 180);
  }
  void setArc(float a, float b, float c, float d, float start, float stop){
    this.a = min(a, c);
    this.b = min(b, d);
    this.c = max(a, c);
    this.d = max(b, d);
  }
  PVector getCentre(){
    float cx = (this.c - this.a)/2.0;
    float cy = (this.d = this.b)/2.0;
    return new PVector(cx, cy);
  }
  boolean isBetweenInc(float v, float lo, float hi){
    if(v >= lo && v <= hi) return true;
  return false;
  }
  boolean isPointInside(PVector p){
    if(isBetweenInc(p.x, this.a, this.c) && isBetweenInc(p.y, this.b, this.d))return true;
    return false;
  }
  float getWidth(){
    return(this.c - this.a);
  }
  float getHeight(){
    return(this.d - this.b);
  }
}

Solution

  • I assume that the angle of the arc is set in degrees:

    setArc(p1.x, p1.y, p2.x, p2.y, 90, 180);
    

    But the angles have to be passed to the arc() function in radians rather than degrees. Use radians() to convert from degrees to radians.

    e.g.

    class UIArc{
    
        // [...]
    
        void setArc(float a, float b, float c, float d, float start, float stop){
            this.a = min(a, c);
            this.b = min(b, d);
            this.c = max(a, c);
            this.d = max(b, d);
            this.start = start;
            this.stop = stop;
        }
    
        void draw() {
            arc(this.a, this.b, this.c, this.c,
            radians(this.start), radians(this.stop));
        }
    }