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

  1. Procedure CornerTool;
  2. {
  3. ⌐1997, Diehl Graphsoft, Inc.
  4.  
  5. This procedure extends or trims lines to form sharp corners.
  6. Developed by Tom Urie.
  7. }
  8. LABEL 10,15,80,99;
  9.  
  10. VAR
  11.     b1,b2,m1,m2 : REAL;
  12.     xt,yt,CursorX,CursorY,px,py : REAL;
  13.     x1,x2,y1,y2,xp,yp,x1n,x2n,y1n,y2n : ARRAY[1..2] OF REAL;
  14.  
  15.     j,k,ObjType,KeyCode : INTEGER;
  16.     Case : ARRAY[1..2] OF INTEGER;
  17.     R,G,B : LONGINT;
  18.  
  19.     ObjectH,nObjectH : ARRAY[1..3] OF HANDLE;
  20.  
  21.     FirstLine,FirstTime : BOOLEAN;
  22.  
  23. Procedure Swap(VAR x1,x2,y1,y2 : REAL);
  24. {
  25. This procedure swaps the values of x1 and x2, and y1 and y2.
  26. }
  27. VAR
  28.     Temp : REAL;
  29.  
  30. BEGIN
  31.     Temp:=x1;
  32.     x1:=x2;
  33.     x2:=Temp;
  34.     Temp:=y1;
  35.     y1:=y2;
  36.     y2:=Temp;
  37. END;
  38.  
  39. BEGIN
  40. {
  41. Main program.
  42. }
  43.  
  44. {
  45. Select the lines.
  46. }
  47.  
  48.     FirstTime:=TRUE;
  49.     10:Message('Select the first line. Click anywhere to end.');
  50.     FirstLine:=FALSE;
  51.     DSelectAll;
  52.     FOR k:=1 TO 2 DO
  53.     BEGIN
  54.         SetCursor(ArrowC);
  55.         REPEAT
  56.  
  57. {
  58. Perform the next two routines if the Esc key is pressed.
  59. }
  60.  
  61.             IF KeyDown(KeyCode) THEN
  62.             BEGIN
  63.                 IF KeyCode = 27 THEN
  64.                 BEGIN
  65.                     IF FirstLine THEN
  66.  
  67. {
  68. (1) If only the first line has been selected, de-select it.
  69. }
  70.  
  71.                         DSelectAll
  72.                     ELSE IF NOT FirstTime THEN
  73.                     BEGIN
  74.  
  75. {
  76. (2) If the new lines have been drawn, restore old lines.
  77. }
  78.  
  79.                         Absolute;
  80.                         DSelectAll;
  81.                         FOR j:=1 TO 2 DO
  82.                         BEGIN
  83.                             MoveTo(x1[j],y1[j]);
  84.                             LineTo(x2[j],y2[j]);
  85.                             ObjectH[j]:=LSActLayer;
  86.  
  87.                             SetLW(ObjectH[j],GetLW(nObjectH[j]));
  88.                             SetLS(ObjectH[j],GetLS(nObjectH[j]));
  89.                             SetClass(ObjectH[j],GetClass(nObjectH[j]));
  90.  
  91.                             GetPenFore((nObjectH[j]),R,G,B);
  92.                             SetPenFore((ObjectH[j]),R,G,B);
  93.  
  94.                             GetPenBack((nObjectH[j]),R,G,B);
  95.                             SetPenBack((ObjectH[j]),R,G,B);
  96.  
  97.                             GetFillFore((nObjectH[j]),R,G,B);
  98.                             SetFillFore((ObjectH[j]),R,G,B);
  99.  
  100.                             GetFillBack((nObjectH[j]),R,G,B);
  101.                             SetFillBack((ObjectH[j]),R,G,B);
  102.  
  103.                             DelObject(nObjectH[j]);
  104.                         END;
  105.                         RedrawAll;
  106.                         DSelectAll;
  107.                         FirstTime:=TRUE;
  108.                     END;    {of NOT FirstTime routine}
  109.                 GOTO 10;
  110.                 END;        {of KeyCode 27 routine}
  111.             END;            {of KeyDown routine}
  112.  
  113. {
  114. Change the cursor to a cross when the mouse is in the vicinity of a line.
  115. }
  116.  
  117.             GetMouse(CursorX,CursorY);
  118.             ObjectH[3]:=PickObject(CursorX,CursorY);
  119.             IF ObjectH[3] <> NIL THEN
  120.             BEGIN
  121.                 ObjType:=GetType(ObjectH[3]);
  122.                 IF ObjType = 2  THEN
  123.                     SetCursor(LgCrossC);
  124.             END ELSE
  125.                 SetCursor(ArrowC);
  126.         UNTIL MouseDown(xp[k],yp[k]);
  127.  
  128. {
  129. Select the object the mouse was on when the mouse was clicked and determine if it was a line.
  130. }
  131.  
  132.         ObjectH[k]:=PickObject(xp[k],yp[k]);
  133.         IF ObjectH[k] = NIL THEN
  134.         BEGIN
  135.             ClrMessage;
  136.             GOTO 99;
  137.         END ELSE
  138.  
  139. {
  140. If the object selected was not a line, end program.
  141. }
  142.  
  143.         BEGIN
  144.             ObjType:=GetType(ObjectH[k]);
  145.             IF ObjType <> 2 THEN BEGIN
  146.                 ClrMessage;
  147.                 Sysbeep;
  148.                 AlrtDialog('The object selected is not a line!');
  149.                 GOTO 99;
  150.             END ELSE
  151.  
  152. {
  153. If the object is a line, get coordinates of end points. Set the leftmost end to x1 and y1, or if the line is vertical, set the lowest end to x1 and y1.
  154. }
  155.  
  156.             BEGIN
  157.                 SetSelect(ObjectH[k]);
  158.                 GetSegPt1(ObjectH[k],x1[k],y1[k]);
  159.                 GetSegPt2(ObjectH[k],x2[k],y2[k]);
  160.                 IF (x2[k] < x1[k]) OR ((x2[k] = x1[k]) AND (y2[k] < y1[k])) THEN
  161.                     Swap(x1[k],x2[k],y1[k],y2[k]);
  162.             END;
  163.         END;
  164.         RedrawAll;
  165.  
  166.         Message('Select the second line. Click anywhere to end.');
  167.         FirstLine:=TRUE;
  168.  
  169.     END;        {of Select Lines}
  170.     ClrMessage;
  171.  
  172. {
  173. Determine the equations of the lines and find the coordinates of the intersection point.
  174. }
  175.  
  176.     IF x2[1] <> x1[1] THEN
  177.     BEGIN
  178.         Case[1]:=1;
  179.         m1:=(y2[1] - y1[1])/(x2[1] - x1[1]);
  180.         b1:=y1[1] - m1*x1[1];
  181.     END  ELSE
  182.     BEGIN
  183.         Case[1]:=2;
  184.         xt:=x1[1];
  185.         IF x2[2] <> x1[2] THEN
  186.         BEGIN
  187.             Case[2]:=1;
  188.             m2:=(y2[2] - y1[2])/(x2[2] - x1[2]);
  189.             b2:=y1[2] - m2*x1[2];
  190.             yt:=m2*xt+b2;
  191.             GOTO 80;
  192.         END ELSE
  193.  
  194. {
  195. If both lines are vertical give warning.
  196. }
  197.  
  198.         BEGIN
  199.             Sysbeep;
  200.             AlrtDialog('Those lines do not intersect!');
  201.             GOTO 10;
  202.         END;
  203.     END;
  204.  
  205.     IF x2[2] <> x1[2] THEN
  206.     BEGIN
  207.         Case[2]:=1;
  208.         m2:=(y2[2] - y1[2])/(x2[2] - x1[2]);
  209.         b2:=y1[2] - m2*x1[2];
  210.     END ELSE
  211.     BEGIN
  212.         Case[2]:=2;
  213.         xt:=x2[2];
  214.         yt:=m1*xt+b1;
  215.         GOTO 80;
  216.     END;
  217.  
  218.     IF m1 <> m2 THEN
  219.     BEGIN
  220.         xt:=(b2-b1)/(m1-m2);
  221.         yt:=(m1*b2-m2*b1)/(m1-m2);
  222.         GOTO 80;
  223.     END ELSE
  224.  
  225. {
  226. IF the lines are parallel give warning.
  227. }
  228.  
  229.     BEGIN
  230.         Sysbeep;
  231.         AlrtDialog('Those lines do not intersect!');
  232.         GOTO 10;
  233.     END;
  234.  
  235. {
  236. Determine the new end points of the lines. The portion of the line that remains is determined by the location of the mouse when the line was selected.
  237. }
  238.  
  239.     80:FOR k:=1 TO 2 DO
  240.     BEGIN
  241.         IF Case[k] = 1 THEN
  242.         BEGIN
  243.             IF xp[k] < xt THEN
  244.             BEGIN
  245.                 x1n[k]:=x1[k];    y1n[k]:=y1[k];
  246.                 x2n[k]:=xt;                y2n[k]:=yt;
  247.             END ELSE
  248.             BEGIN
  249.                 x1n[k]:=xt;                y1n[k]:=yt;
  250.                 x2n[k]:=x2[k];    y2n[k]:=y2[k];
  251.             END;
  252.         END
  253.         ELSE BEGIN
  254.             IF yp[k] < yt THEN
  255.             BEGIN
  256.                 x1n[k]:=x1[k];    y1n[k]:=y1[k];
  257.                 x2n[k]:=x2[k];     y2n[k]:=yt;
  258.             END ELSE
  259.             BEGIN
  260.                 x1n[k]:=x1[k];    y1n[k]:=yt;
  261.                 x2n[k]:=x2[k];    y2n[k]:=y2[k];
  262.             END;
  263.         END;
  264.     END;
  265.  
  266. {
  267. Delete the old lines and draw the trimmed lines in their place.
  268. }
  269.  
  270.     Absolute;
  271.     DSelectAll;
  272.     FOR k:=1 TO 2 DO
  273.     BEGIN
  274.         MoveTo(x1n[k],y1n[k]);
  275.         LineTo(x2n[k],y2n[k]);
  276.         nObjectH[k]:=LSActLayer;
  277.  
  278.         SetLW(nObjectH[k],GetLW(ObjectH[k]));
  279.         SetLS(nObjectH[k],GetLS(ObjectH[k]));
  280.         SetClass(nObjectH[k],GetClass(ObjectH[k]));
  281.  
  282.         GetPenFore((ObjectH[k]),R,G,B);
  283.         SetPenFore((nObjectH[k]),R,G,B);
  284.  
  285.         GetPenBack((ObjectH[k]),R,G,B);
  286.         SetPenBack((nObjectH[k]),R,G,B);
  287.  
  288.         GetFillFore((ObjectH[k]),R,G,B);
  289.         SetFillFore((nObjectH[k]),R,G,B);
  290.  
  291.         GetFillBack((ObjectH[k]),R,G,B);
  292.         SetFillBack((nObjectH[k]),R,G,B);
  293.  
  294.         DelObject(ObjectH[k]);
  295.  
  296.     END;
  297.  
  298.     RedrawAll;
  299.     FirstTime:=FALSE;
  300.     GOTO 10;
  301.  
  302. 99:END;
  303.  
  304. RUN(CornerTool);