summaryrefslogtreecommitdiff
path: root/app/mpg/2.2/src/terminal plot
blob: 21a17ff30ea7f30806c549a3c0c39277eb257b98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
(* Prozeduren zur Ausgabe auf ASCII-Terminals *)
INT CONST up         :=  1 ,
          right      :=  1 ,
          down       := -1 ,
          left       := -1 ;
 
INT VAR x pos := 0 ,
        y pos := 0 ,
        new x pos ,
        new y pos ;
 
BOOL VAR plot := FALSE;
TEXT CONST empty line :: 79 * " ";
ROW 24 TEXT VAR display;
 
 
PROC plot vector (INT CONST dx , dy) :
 
  IF dx >= 0
    THEN IF   dy >  dx THEN vector (y pos, x pos, dy, dx, up, right)
         ELIF dy >   0 THEN vector (x pos, y pos, dx, dy, right, up)
 
         ELIF dy > -dx THEN vector (x pos, y pos, dx, -dy, right, down)
         ELSE               vector (y pos, x pos, -dy, dx, down, right)
         FI
    ELSE IF   dy > -dx THEN vector (y pos, x pos, dy, -dx, up, left)
         ELIF dy >   0 THEN vector (x pos, y pos, -dx, dy, left, up)
 
         ELIF dy >  dx THEN vector (x pos, y pos, -dx, -dy, left, down)
         ELSE               vector (y pos, x pos, -dy, -dx, down, left)
         FI
  FI .
 
ENDPROC plot vector ;
 
PROC vector (INT VAR x pos, y pos, INT CONST dx, dy, right, up) :

  prepare first step ;
  INT VAR i ;
  FOR i FROM 1 UPTO dx REP
    do one step
  PER .
 
prepare first step :
  point;
  INT VAR old error := 0 ,
          up right error := dy - dx ,
          right error    := dy .
 
do one step :
  IF right is better
    THEN do right step
    ELSE do up right step
  FI .
 
right is better :
  abs (old error + right error) < abs (old error + up right error) .
 
do upright step :
  x pos INCR right ;
  y pos INCR up ;
  point ;
  old error INCR upright error .
 
do right step :
  x pos INCR right ;
  point ;
  old error INCR right error .
 
ENDPROC vector ;
 
 
PROC point :
  IF x pos < 1
  THEN x pos := 1
  ELIF x pos > 78
  THEN x pos := 78 FI;

  IF y pos < 1
  THEN y pos := 1
  ELIF y pos > 47
  THEN y pos := 47 FI;

  INT CONST line :: y pos DIV 2;
  BOOL CONST above :: (y pos MOD 2) = 1;
  TEXT CONST point :: display [line+1] SUB (x pos+1),
             new point :: calculated point;
 
  replace (display [line+1], x pos+1, new point);
  cursor (x pos, 24-line);
  out (new point) .
 
calculated point :
  IF above
  THEN IF point = "," OR point = "|"
       THEN "|"
       ELSE "'" FI
  ELSE IF point = "'" OR point = "|"
       THEN "|"
       ELSE "," FI
  FI
 
END PROC point;
 
REAL CONST real max int := real (max int);
INT PROC round (REAL CONST x) :
  IF x > real max int
  THEN max int
  ELIF x < 0.0
  THEN 0
  ELSE int (x + 0.5) FI

END PROC round;