Gator_Bks

Program Jam Otomatis Pada Pascal

Jun 18th, 2020
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.41 KB | None | 0 0
  1. program clock;
  2.  
  3. { --------
  4.  
  5.   Analog clock demo for Pascal N-IDE
  6.  
  7.   by: Gator Bks
  8.  
  9. }
  10.  
  11. uses
  12.  
  13.   CRT, Graph, SysUtils, DOS, Math;
  14.  
  15.  
  16.  
  17. type
  18.  
  19.     // screen orientation
  20.  
  21.  
  22.  
  23.   TOrientation = (oPortrait, oLandscape);
  24.  
  25.  
  26.  
  27.     // clock structure
  28.  
  29.  
  30.  
  31.   TPoint = record
  32.  
  33.     x, y : integer;
  34.  
  35.     // x,y coordinat
  36.  
  37.   end;
  38.  
  39.  
  40.  
  41.   TAngle = record
  42.  
  43.     a : float;
  44.  
  45.     // angle
  46.  
  47.     r : integer;
  48.  
  49.     // radius
  50.  
  51.   end;
  52.  
  53.  
  54.  
  55. var
  56.  
  57.   o : TOrientation;
  58.  
  59.   c : TPoint;
  60.  
  61.   a : TAngle;
  62.  
  63.  
  64.  
  65. // color selection shortcut
  66.  
  67. function color(r, g, b : byte) : integer;
  68.  
  69. var
  70.  
  71.   cs : android_graphics_Color;
  72.  
  73.   c : integer;
  74.  
  75. begin
  76.  
  77.   c := cs.rgb(r, g, b);
  78.  
  79.   color := c;
  80.  
  81. end;
  82.  
  83.  
  84.  
  85. // angle (degree) point inside a circle
  86.  
  87. function point(c : TPoint; a : TAngle) : TPoint;
  88.  
  89. var
  90.  
  91.   r : TPoint;
  92.  
  93. begin
  94.  
  95.   r.x := round(c.x + a.r * sin(degToRad(a.a)));
  96.  
  97.   r.y := round(c.y + a.r * cos(degToRad(a.a)));
  98.  
  99.   point := r;
  100.  
  101. end;
  102.  
  103.  
  104.  
  105. // clock-wise point inside a circle
  106.  
  107. function hand(c : TPoint; a : TAngle) : TPoint;
  108.  
  109. var
  110.  
  111.   p : TPoint;
  112.  
  113. begin
  114.  
  115.   a.a := -a.a - 180; // adjust to screen
  116.  
  117.   p := point(c, a);
  118.  
  119.   hand := p;
  120.  
  121. end;
  122.  
  123.  
  124.  
  125. procedure drawText;
  126.  
  127. var
  128.  
  129.   t : string = 'Analog Clock';
  130.  
  131.   h, n, s, z, w : Word;
  132.  
  133.   j, m, d, e : string;
  134.  
  135.   yr, mo, dy, wd : Word;
  136.  
  137. begin
  138.  
  139.   setTextStyle(defaultFont, horizDir, 3);
  140.  
  141.   setTextJustify(leftText, topText);
  142.  
  143.   setColor(15);
  144.  
  145.   w := textWidth(t) div 2;
  146.  
  147.   if o = oPortrait then
  148.  
  149.     outTextXY(c.x - w, c.y - a.r - 50, t)
  150.  
  151.   else
  152.  
  153.     outTextXY(c.x - a.r - w * 2 - 50, c.y, t);
  154.  
  155.   // show digital time
  156.  
  157.   getTime(h, n, s, z);
  158.  
  159.   j := intToStr(h);
  160.  
  161.   if length(j) = 1 then j := '0' + j;
  162.  
  163.   m := intToStr(n);
  164.  
  165.   if length(m) = 1 then m := '0' + m;
  166.  
  167.   d := intToStr(s);
  168.  
  169.   if length(d) = 1 then d := '0' + d;
  170.  
  171.   e := intToStr(z);
  172.  
  173.   if length(e) = 1 then e := '0' + e;
  174.  
  175.   t := j + ':' + m + ':' + d + '.' + e;
  176.  
  177.   w := textWidth(t) div 2;
  178.  
  179.   if o = oPortrait then
  180.  
  181.     outTextXY(c.x - w, c.y + a.r + 80, t)
  182.  
  183.   else
  184.  
  185.     outTextXY(c.x + a.r + 50, c.y, t);
  186.  
  187.   getDate(yr, mo, dy, wd);
  188.  
  189.   // FORMAT DOES NOT WORK!!!
  190.  
  191.   //t := format('%-2.2d-%-2.2d-%-2.2d',[dy,mo,yr]);
  192.  
  193.   //outTextXY(c.x-w,c.y+a.r+140,t)
  194.  
  195. end;
  196.  
  197.  
  198.  
  199. procedure handHour(h, n : integer);
  200.  
  201. var
  202.  
  203.   p : TPoint;
  204.  
  205.   r : TAngle;
  206.  
  207. begin
  208.  
  209.   if h > 11 then h := h - 12;
  210.  
  211.   r := a;
  212.  
  213.   r.a := (h + n / 60) * 30;
  214.  
  215.   r.r := r.r div 2 - 12;
  216.  
  217.   p := hand(c, r);
  218.  
  219.   // hand
  220.  
  221.   setColor(lightBlue);
  222.  
  223.   setLineStyle(solidLn, 0, 24);
  224.  
  225.   setFillStyle(solidFill, lightBlue);
  226.  
  227.   line(c.x, c.y, p.x, p.y);
  228.  
  229.   // rounded tip
  230.  
  231.   setLineStyle(solidLn, 0, 1);
  232.  
  233.   //pieSlice(p.x,p.y,0,360,11);
  234.  
  235. end;
  236.  
  237.  
  238.  
  239. procedure handMinute(n, s : integer);
  240.  
  241. var
  242.  
  243.   p : TPoint;
  244.  
  245.   r : TAngle;
  246.  
  247. begin
  248.  
  249.   r := a;
  250.  
  251.   r.a := (n + (s / 60)) * 6;
  252.  
  253.   r.r := r.r - 50;
  254.  
  255.   p := hand(c, r);
  256.  
  257.   // hand
  258.  
  259.   setColor(lightGreen);
  260.  
  261.   setLineStyle(solidLn, 0, 12);
  262.  
  263.   setFillStyle(solidFill, lightGreen);
  264.  
  265.   line(c.x, c.y, p.x, p.y);
  266.  
  267.   // rounded tip
  268.  
  269.   setLineStyle(solidLn, 0, 1);
  270.  
  271.   //pieSlice(p.x,p.y,0,360,5);
  272.  
  273. end;
  274.  
  275.  
  276.  
  277. procedure handSecond(s, z : integer);
  278.  
  279. var
  280.  
  281.   p : TPoint;
  282.  
  283.   r : TAngle;
  284.  
  285. begin
  286.  
  287.   r := a;
  288.  
  289.   r.a := (s + (z / 100)) * 6;
  290.  
  291.   setColor(red);
  292.  
  293.   setFillStyle(solidFill, red);
  294.  
  295.   // hand
  296.  
  297.   r.r := a.r - 20;
  298.  
  299.   p := hand(c, r);
  300.  
  301.   setLineStyle(solidLn, 0, 4);
  302.  
  303.   line(c.x, c.y, p.x, p.y);
  304.  
  305.   // circle tip
  306.  
  307.   {r.r := a.r-30;
  308.  
  309.   p := hand(c,r);
  310.  
  311.   circle(p.x,p.y,10);}
  312.  
  313.   // calculate tail
  314.  
  315.   r.a := r.a - 180;
  316.  
  317.   r.r := 60;
  318.  
  319.   p := hand(c, r);
  320.  
  321.   // tail
  322.  
  323.   setLineStyle(solidLn, 0, 4);
  324.  
  325.   line(c.x, c.y, p.x, p.y);
  326.  
  327.   // center
  328.  
  329.   pieSlice(c.x, c.y, 0, 360, 20);
  330.  
  331. end;
  332.  
  333.  
  334.  
  335. procedure drawFace;
  336.  
  337. var
  338.  
  339.   i : integer;
  340.  
  341.   p : TPoint;
  342.  
  343.   r : TAngle;
  344.  
  345. begin
  346.  
  347.   // style
  348.  
  349.   setTextJustify(centerText, centerText);
  350.  
  351.   setTextStyle(defaultFont, horizDir, 3);
  352.  
  353.   setLineStyle(solidLn, 0, 2);
  354.  
  355.   setFillStyle(solidFill, color(24, 24, 24));
  356.  
  357.   setColor(7);
  358.  
  359.   r := a;
  360.  
  361.   // border
  362.  
  363.   //circle(c.x,c.y,a.r);
  364.  
  365.   pieSlice(c.x, c.y, 0, 360, a.r);
  366.  
  367.   // center
  368.  
  369.   //circle(c.x,c.y,10);
  370.  
  371.   // tick marks
  372.  
  373.   i := 0;
  374.  
  375.   r.r := r.r - 30;
  376.  
  377.   repeat
  378.  
  379.     i := i + 1;
  380.  
  381.     r.a := i * 6; // second degree
  382.  
  383.     p := hand(c, r);
  384.  
  385.       // hour ticks
  386.  
  387.     if i * 6 mod 30 = 0 then
  388.  
  389.       fillEllipse(p.x, p.y, 1, 1)
  390.  
  391.     else
  392.  
  393.       putPixel(p.x, p.y, 7);
  394.  
  395.   until i > 60;
  396.  
  397.   // hour marks
  398.  
  399.   i := 0;
  400.  
  401.   r.r := r.r - 30;
  402.  
  403.   repeat
  404.  
  405.     i := i + 1;
  406.  
  407.     r.a := i * 30; // hour degree
  408.  
  409.     p := hand(c, r);
  410.  
  411.     outTextXY(p.x, p.y, intToStr(i));
  412.  
  413.   until i >= 12;
  414.  
  415. end;
  416.  
  417.  
  418.  
  419. procedure getOrientation;
  420.  
  421. begin
  422.  
  423.   c.x := getMaxX div 2;
  424.  
  425.   c.y := getMaxY div 2;
  426.  
  427.   if c.x > c.y then o := oLandscape
  428.  
  429.   else o := oPortrait;
  430.  
  431.   if o = oLandscape then
  432.  
  433.     a.r := c.y - 10 else a.r := c.x - 10;
  434.  
  435. end;
  436.  
  437.  
  438.  
  439. procedure openScreen;
  440.  
  441. var
  442.  
  443.   gd, gm : integer;
  444.  
  445. begin
  446.  
  447.   gd := detect;
  448.  
  449.   setBufferEnable(true);
  450.  
  451.   initGraph(gd, gm, '');
  452.  
  453. end;
  454.  
  455.  
  456.  
  457. procedure closeScreen;
  458.  
  459. begin
  460.  
  461.   closeGraph;
  462.  
  463. end;
  464.  
  465.  
  466.  
  467. var
  468.  
  469.   h, n, s, z : word;
  470.  
  471. begin
  472.  
  473.   openScreen;
  474.  
  475.   getOrientation;
  476.  
  477.  
  478.  
  479.   repeat
  480.  
  481.     clearBuffer;
  482.  
  483.     drawText;
  484.  
  485.     drawFace;
  486.  
  487.     getTime(h, n, s, z);
  488.  
  489.  
  490.  
  491.     handHour(h, n);
  492.  
  493.     handMinute(n, s);
  494.  
  495.     handSecond(s, z);
  496.  
  497.     drawBuffer;
  498.  
  499.   until keyPressed;
  500.  
  501.  
  502.  
  503.   closeScreen;
  504.  
  505. end.
Advertisement
Add Comment
Please, Sign In to add comment