package curve;
public class BezierCurvePatch extends BezierCurve {
  private BezierCurvePatch previousPatch,nextPatch;
  
  //Does the same as Bezier curve, and sets up links
  public BezierCurvePatch(DrawCurve aDrawCurve,
    BezierCurvePatch aPreviousPatch) {
    super(aDrawCurve,4);//Use 4 point patches
    previousPatch=aPreviousPatch;
    if(previousPatch!=null) previousPatch.setNextPatch(this);
    nextPatch=null;
  };
  
  public void setNextPatch(BezierCurvePatch aNextPatch) {
    nextPatch=aNextPatch;
  };
  
  //fuction to set a marker, override to change linked patches
  public void setMarker(int i,double x,double y) {
    int ctr;
    Coordinate co;
    
    if(marker!=null) //array initialised
    if(i>=0 && i<marker.length) {
      marker[i].x=x;
      marker[i].y=y;
      xText[i].setText(new PrintableDouble(x).asStringSF(3));
      yText[i].setText(new PrintableDouble(y).asStringSF(3));
    };

    if(previousPatch!=null && (i==0 || i==1)) //Change previous curve
      previousPatch.adjustPrevious(marker[0],marker[1]);
    if(nextPatch!=null && (i==2 || i==3)) //Change next curve
      nextPatch.adjustNext(marker[2],marker[3]);
  };
  
  public void adjustPrevious(Coordinate marker0, Coordinate marker1) {
    marker[3]=new Coordinate(marker0);
    marker[2]=new Coordinate(marker0);
    marker[2].mul(2);
    marker[2].sub(marker1);
    for(int ctr=2;ctr<=3;ctr++) {
      xText[ctr].setText(new PrintableDouble(marker[ctr].x).asStringSF(3));
      yText[ctr].setText(new PrintableDouble(marker[ctr].y).asStringSF(3));
    };
  };
  
  public void adjustNext(Coordinate marker2, Coordinate marker3) {
    marker[0]=new Coordinate(marker3);
    marker[1]=new Coordinate(marker3);
    marker[1].mul(2);
    marker[1].sub(marker2);
    for(int ctr=0;ctr<=1;ctr++) {
      xText[ctr].setText(new PrintableDouble(marker[ctr].x).asStringSF(3));
      yText[ctr].setText(new PrintableDouble(marker[ctr].y).asStringSF(3));
    };
  };
}