# === mkdraw script: construction de rectangle avec coins arrondis ===
#  JMB le 11.07.24


#====== Box with rounded corners. #======
# on construit en tournant dans le sens trigo
# %x0,%y0 is bottom left corner point
# %w,%h are width and height
# %rx, %ry  are corner radius x, y
define rect_rounded [ %x0 %y0 %w %h %rx %ry ] {
	move  %x0    ( %y0 + %h - %ry )
	curve  %x0 ( %y0 + %h - ( %ry * 0.45 ) ) ( %x0 + %rx * 0.45 )  ( %y0 + %h )  ( %x0 + %rx ) ( %y0 + %h )
	line    ( %x0 +  %w -  %rx )   ( %y0 + %h )
	curve  ( %x0 + %w - ( %rx * 0.45  ) ) ( %y0 + %h ) ( %x0 + %w ) ( %y0 + %h - ( %ry * 0.45  ) ) ( %x0 + %w ) ( %y0 + %h - %ry )
	line  ( %x0 + %w ) ( %y0 + %ry )
	curve ( %x0 + %w ) ( %y0 + ( %ry * 0.45 ) ) ( %x0 + %w - ( %rx * 0.45 ) ) %y0 ( %x0 + %w - %rx ) %y0
	line  ( %x0 + %rx ) %y0
	curve ( %x0 + ( %rx * 0.45 ) ) %y0 %x0 ( %y0 + ( %ry * 0.45 ) ) %x0 ( %y0 + %ry )
	line  %x0 ( %y0 + %h - %ry )
	close
}

#====== Rectangle ======
# %x0,%y0 left low corner
# %h, %w  hight, width
define rect_hw [ %x0 %y0 %w %h ] {
	Move  %x0 %y0
	Line ( ( %x0        %y0 + %h  ) )
	Line ( ( %x0 + %w   %y0 + %h  ) )
	Line ( ( %x0 + %w   %y0       ) )
	Line ( ( %x0        %y0       ) )
	Close
}


#====== circle ======
# %cx, %cy  are circle center.
# %r  is circle radius
define circle [ %cx %cy %r ] {
	move ( %cx + %r )  %cy
	arc %cx %cy  $pi/2
	arc %cx %cy  $pi/2
	arc %cx %cy  $pi/2
	arc %cx %cy  $pi/2
	close
}

#====== ellipse ======
# %cx, %cy  are ellipse center.
# %rx, %ry  are ellipse radius x, y
define ellips [ %cx %cy %rx %ry ] {
	move ( %cx + %rx )   %cy
	curve ( %cx + %rx )   %cy  ( %cx + %rx ) ( %cy + %ry )  %cx ( %cy + %ry )
	curve %cx ( %cy + %ry ) ( %cx - %rx ) ( %cy + %ry )  ( %cx - %rx )  %cy
	curve ( %cx - %rx )  %cy   ( %cx - %rx ) ( %cy - %ry )   %cx  ( %cy - %ry )
	curve %cx  ( %cy - %ry )  ( %cx + %rx ) ( %cy - %ry  )  ( %cx + %rx ) %cy
	close
}


#==================  Arc  ( Ellipse)  ================
# %x0,%y0 is centre
# %rx     is radius x
# %ry     is radius y
# %a0,    is pseudo starting angle in radians
# %da     is pseudo delta from a%
# %n      if set, indicates how many
#         arcs to use
# %teta angle entre axe des x et le grand axe


Define Arc7 {


  # limitation  2Pi
  IFLess %a0 ( 2 * $Pi )
    Else
    Set %a0 ( %a0 - ( 2 * $Pi ) )
  Endif

  IFLess ( Abs %da ) ( 2 * $Pi )
    Else
    Set %da (  2 * $Pi )
  Endif

  Set $a0 %a0

  IfEqual %da $Pi/2
    Set %n 1
    Else
    Set %n Floor  ( ( ABS %da ) * 2 / $Pi + 1 )
  Endif
  Set $n %n
  Set %da ( %da / %n )
  Set $da %da

#------------   construction des arcs successifs ------------
  Set %aa %a0
  For %k 0 %n {
    Set %a %aa            # P1
    Set %aa ( %a + %da )  # P2
    IfLess ( Abs %aa ) ( 2 * $Pi )
      Else
      Set %aa ( %aa - 2 * $Pi )
    Endif

 #Calcul de l'angle d'entre eta1 avec %a

    Set %eta1 %a
# Set %eta1 (  Arctan ( ( %rx / %ry ) * ( tan %a  )   )  )
#    IfLess  ( Abs %a ) $Pi/2
#       #Set %eta1 %eta1
#      Else
#      IFLess  ( Abs %a ) ( $Pi * 3 / 2 )
#        Set %eta1 ( %eta1 + $Pi )
#      Endif
#      IfEqual  ( Abs %a ) ( $Pi  / 2 )
#         Set %eta1 ( %eta1 + $Pi )
#      Endif
#      IfEqual  ( Abs %a ) ( $Pi * 3 / 2 )
#         Set %eta1 ( %eta1 + $Pi )
#      Endif
#    EndIf

 #Calcul de l'angle de sortie eta2 avec %aa = %a + %da
#  Set %eta2 (  Arctan ( ( %rx / %ry ) * ( tan %aa  )   )  )
#    IfLess ( Abs %aa )  $Pi/2   # aa < Pi
#       #Set %eta2 %eta2
#       Else
#       IFLess ( Abs %aa )   ( $Pi * 3 / 2 )   # Pi <= aa < 3Pi/2
#         Set %eta2 ( %eta2 + $Pi )
#       EndIf
#       IfEqual ( Abs %aa ) ( $Pi  / 2 )
#          Set %eta2 ( %eta2 + $Pi )
#       Endif
#       IfEqual ( Abs %aa ) ( $Pi * 3 / 2 )
#          Set %eta2 ( %eta2 + $Pi )
#       Endif
#    Endif
    Set %eta2 %aa

 # calcul du point P1
    Set %P1x  ( %x0 + %rx * (  cos %teta ) * ( cos %eta1 )  - %ry * ( sin %teta )  * ( sin %eta1 ) )
    Set %P1y  ( %y0 + %rx * (  sin %teta ) * ( cos %eta1 )  + %ry * ( cos %teta )  * ( sin %eta1 ) )

 # calcul du point P2
    Set %P2x  ( %x0 + %rx * (  cos %teta ) * ( cos %eta2 )  - %ry * ( sin %teta )  * ( sin %eta2 ) )
    Set %P2y  ( %y0 + %rx * (  sin %teta ) * ( cos %eta2 )  + %ry * ( cos %teta )  * ( sin %eta2 ) )

  #point d'entre


  IfEqual %k 0
    Move %P1x %P1y
  EndIf

#-----------------   #pts de contrle -------------
 #calcul de alpha
   Set %ta  tan ( ( %eta2 - %eta1 ) / 2 )
   Set %ta2 ( %ta * %ta )
   Set %radical ( ( ( sqrt ( 4 + ( 3 * %ta2 ) ) ) - 1 )  / 3 )
   Set %sinus  sin ( %eta2 - %eta1 )
   Set %alpha ( %sinus * %radical )

 # pt de contrle Q1
   Set %dE1x ( ( - %rx * ( cos %teta ) * ( sin %eta1 ) ) - ( %ry  * ( sin %teta )  * ( cos %eta1 ) ) )
   Set %dE1y ( ( - %rx * ( sin %teta ) * ( sin %eta1 ) ) + ( %ry  * ( cos %teta )  * ( cos %eta1 ) ) )
   Set %Q1x (  %P1x + %alpha * %dE1x  )
   Set %Q1y (  %P1y + %alpha * %dE1y  )

  # pt de contrle Q2
   Set %dE2x ( ( - %rx * ( cos %teta ) * ( sin %eta2 ) ) - ( %ry  * ( sin %teta )  * ( cos %eta2 ) ) )
   Set %dE2y ( ( - %rx * ( sin %teta ) * ( sin %eta2 ) ) + ( %ry  * ( cos %teta )  * ( cos %eta2 ) ) )
   Set %Q2x (  %P2x - %alpha * %dE2x  )
   Set %Q2y ( %P2y - %alpha * %dE2y  )

 # curve attend 6 paramtres
   Curve %Q1x %Q1y %Q2x %Q2y %P2x %P2y
  } #fin du for

}
#---------------  fin du trac ellipse  --------------
