home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 March / Chip_1998-03_cd.bin / tema / MINICAD / MC7DEMO / MINICAD.1 / OFFSET.MPC < prev    next >
Text File  |  1997-04-30  |  4KB  |  226 lines

  1. Procedure DrawCurve;
  2.  
  3. LABEL 99;
  4.  
  5. CONST
  6.     MaxPoints = 10;
  7.     ec = 0.25;
  8.  
  9. VAR
  10.     a,b1,b2,e,L,r11,r12,Phi1,Theta1 : REAL;
  11.     c1,c2,h1,h2,r21,r22,Phi2,Theta2 : REAL;
  12.     hMin,p1,p2,q1,q2 : REAL;
  13.     x0,y0,x11,y11,x12,y12 : REAL;
  14.     x21,y21,x22,y22,Alpha : REAL;
  15.     x,y,xp,yp,R : ARRAY[1..MaxPoints] OF REAL;
  16.     k, nPoints : INTEGER;
  17.  
  18. Procedure Swap(VAR v1,v2:REAL);
  19. VAR
  20.     Temp : REAL;
  21.  
  22. BEGIN
  23.     Temp:=v1;
  24.     v1:=v2;
  25.     v2:=Temp;
  26. END;
  27.  
  28. Procedure DrawPolyPoint(x,y,R : REAL);
  29. {
  30. This procedure draws a polyline point based on the value of R:
  31. R = 0    ==> Corner point
  32. R > 0    ==> Arc point of radius, R
  33. R = -1    ==> Cubic spline point
  34. R = any value less than 0 except -1
  35.             ==> Bezier control point
  36. }
  37.  
  38. BEGIN
  39.     IF R = 0 THEN
  40.         LineTo(x,y)
  41.     ELSE IF R > 0 THEN
  42.         ArcTo(x,y,R)
  43.     ELSE IF R = -1 THEN
  44.         CurveThrough(x,y)
  45.     ELSE
  46.         CurveTo(x,y);
  47. END;    {of DrawPolyPoint}
  48.  
  49. Function xt(x,y,x0,y0,Alpha:REAL) : REAL;
  50. {
  51. This function transforms the x-coordinate of a point relative to the x,y axis to an axis passing through x0,y0 at an angle, Alpha.
  52. }
  53.  
  54. VAR
  55.     A : REAL;
  56.  
  57. BEGIN
  58.     A:=Deg2Rad(Alpha);
  59.     xt:=x0 + x*Cos(A) - y*Sin(A);
  60. END;    {of Function xt}
  61.  
  62. Function yt(x,y,x0,y0,Alpha:REAL) : REAL;
  63. {
  64. This function transforms the y-coordinate of a point relative to the x,y axis to an axis passing through x0,y0 at an angle, Alpha.
  65. }
  66.  
  67. VAR
  68.     A : REAL;
  69.  
  70. BEGIN
  71.     A:=Deg2Rad(Alpha);
  72.     yt:=y0 + y*Cos(A) + x*Sin(A);
  73. END;    {of Function yt}
  74.  
  75. {
  76. Main program.
  77. }
  78. BEGIN
  79.     DSelectAll;
  80.  
  81. {
  82. Get insertion point of object and dimensions a, b, h1, and h2.
  83. }
  84.  
  85.     Message('Draw side A');
  86.     GetPt(x11,y11);
  87.     GetPtL(x11,y11,x12,y12);
  88.     IF y11 < y12 THEN Swap(y11,y12);
  89.  
  90.     Message('Draw side B');
  91.     GetPt(x21,y21);
  92.     GetPtL(x21,y21,x22,y22);
  93.     IF y21 < y22 THEN Swap(y21,y22);
  94.     IF x21 < x11 THEN
  95.     BEGIN
  96.         Swap(x21,x11);
  97.         Swap(y11,y21);
  98.         Swap(y12,y22);
  99.     END;
  100.  
  101.     x0:=x11;
  102.     y0:=y11;
  103.  
  104. {
  105. Get angle of object with respect to x-axis (optional).
  106.  
  107.     GetPtL(x1,y1,x2,y2);
  108.     IF (x1 = x2) AND (y1 = y2) THEN GOTO 99;
  109.     L:=Distance(x1,y1,x2,y2);
  110.     Alpha:=Rad2Deg(ArcCos((x2-x1)/L));
  111.     IF y2 < y1 THEN Alpha:=360 - Alpha;}
  112.  
  113.     a:=x21-x11;
  114.     b1:=y11-y21;
  115.     h1:=y11-y12;
  116.     h2:=y21-y22;
  117.     e:=ec;
  118. Message(a,'   ',b1,'   ',h1,'   ',h2);
  119.  
  120.     hMin:=h1;
  121.     IF h2 < hMin THEN hMin:=h2;
  122.     c1:=Sqrt(a^2 + b1^2);
  123.     Phi1:=ArcSin(b1/c1);
  124.     IF b1 <> 0 THEN
  125.     BEGIN
  126.         r11:=hMin/2 + e*c1/Sin(Phi1);
  127.         r12:=(a^2 + b1^2)/(2*b1) - r11;
  128.         Theta1:=ArcSin(a/(r11+r12));
  129.     END ELSE
  130.     BEGIN
  131.         r11:=0;
  132.         r12:=0;
  133.         Theta1:=0;
  134.     END;
  135.     p1:=r11*Tan(Theta1/2);
  136.     q1:=r12*Tan(Theta1/2);
  137.  
  138.     b2:=b1+h2-h1;
  139.     c2:=Sqrt(a^2 + b2^2);
  140.     Phi2:=ArcSin(b2/c2);
  141.     IF b2 <> 0 THEN
  142.     BEGIN
  143.         r21:=hMin/2 + e*c2/Sin(Phi2);
  144.         r22:=(a^2 + b2^2)/(2*b2) - r21;
  145.         Theta2:=ArcSin(a/(r21+r22));
  146.     END ELSE
  147.     BEGIN
  148.         r21:=0;
  149.         r22:=0;
  150.         Theta2:=0;
  151.     END;
  152.     p2:=r21*Tan(Theta2/2);
  153.     q2:=r22*Tan(Theta2/2);
  154.  
  155. {
  156. Calculate the coordinates of the poly points relative to the insertion point of the object (x0,y0) at an angle of Alpha=0.
  157. }
  158.  
  159.     nPoints:=10;
  160.  
  161.     x[1]:=0;
  162.     y[1]:=0;
  163.     R[1]:=0;
  164.  
  165.     x[2]:=p1;
  166.     y[2]:=0;
  167.     R[2]:=r11;
  168.  
  169.     x[3]:=p1*(1+Cos(Theta1));
  170.     y[3]:=-p1*Sin(Theta1);
  171.     R[3]:=0;
  172.  
  173.     x[4]:=a-q1;
  174.     y[4]:=-b1;
  175.     R[4]:=r12;
  176.  
  177.     x[5]:=a;
  178.     y[5]:=-b1;
  179.     R[5]:=0;
  180.  
  181.     x[6]:=a;
  182.     y[6]:=-(b1+h2);
  183.     R[6]:=0;
  184.  
  185.     x[7]:=a-p2;
  186.     y[7]:=y[6];
  187.     R[7]:=r21;
  188.  
  189.     x[8]:=a-p2*(1+Cos(Theta2));
  190.     y[8]:=y[6]+p2*Sin(Theta2);
  191.     R[8]:=0;
  192.  
  193.     x[9]:=q2;
  194.     y[9]:=-h1;
  195.     R[9]:=r22;
  196.  
  197.     x[10]:=0;
  198.     y[10]:=-h1;
  199.     R[10]:=0;
  200.  
  201. {
  202. Calculate the points relative to the x and y axis of the drawing (0,0) and, if appropiate, adjust for any rotation (Alpha).
  203. }
  204.  
  205.     FOR k:=1 TO nPoints DO
  206.     BEGIN
  207.         xp[k]:=xt(x[k],y[k],x0,y0,Alpha);
  208.         yp[k]:=yt(x[k],y[k],x0,y0,Alpha);
  209.     END;
  210.  
  211. {
  212. Move to the absolute coordinates of the first point and draw the polyline.
  213. }
  214.  
  215.     Absolute;
  216.     MoveTo(x0,y0);
  217.     ClosePoly;
  218.     BeginPoly;
  219.         FOR k:=1 TO nPoints DO
  220.             DrawPolyPoint(xp[k],yp[k],R[k]);
  221.     EndPoly;
  222.  
  223. 99:END;    {of Main program}
  224.  
  225. RUN(DrawCurve);
  226.