Oberon-7 - Circle Segments
Hi, What would be a simple algo to divide a circle into 12 segments, possibly without Math lib? If you are wondering what for, I wish to have a SWATCH clock on my desk :-) -- Tomas Kral <thomas.kral@email.cz>
On Tue, 19 Feb 2019 18:08:54 +0100 Tomas Kral <thomas.kral@email.cz> wrote:
circle into 12 segments
Looking at V4 `ClockElem' I see this PROCEDURE Init; VAR i: INTEGER; BEGIN i := 0; WHILE i < 60 DO sin[i] := Math.sin(2 * Math.pi / 60 * i); cos[i] := Math.cos(2 * Math.pi / 60 * i); INC(i) END END Init; I probably cannot do without trigonometry, right? -- Tomas Kral <thomas.kral@email.cz>
PROCEDURE HourTicks(r: INTEGER); BEGIN s:= r DIV 2; c := r * 181 DIV 209; x[0] := 0; y[0] := r; x[1] := s; y[1] := c; x[2] := c; y[2] := s; x[3] := r; y[3] := 0; x[4] := c; y[4] := -s; x[5] := s; y[5] := -c; x[6] := 0; y[6] := -r; x[7] := -s; y[7] := -c; x[8] := -c; y[8] := -s; x[9] := -r; y[9] := 0; x[10] := -c; y[10] := s; x[11] := -s; y[11] := c; END HourTicks br Jörg
Am 19.02.2019 um 18:23 schrieb Tomas Kral <thomas.kral@email.cz>:
On Tue, 19 Feb 2019 18:08:54 +0100 Tomas Kral <thomas.kral@email.cz> wrote:
circle into 12 segments
Looking at V4 `ClockElem' I see this
PROCEDURE Init; VAR i: INTEGER; BEGIN i := 0; WHILE i < 60 DO sin[i] := Math.sin(2 * Math.pi / 60 * i); cos[i] := Math.cos(2 * Math.pi / 60 * i); INC(i) END END Init;
I probably cannot do without trigonometry, right?
-- Tomas Kral <thomas.kral@email.cz> -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
Tomas The twelve segments - as originally requested - can be done with HourTicks below with INTEGER arithmetic only. According to JR Strohm's suggestion even MinuteTicks can be calculated quite accurately with REALs without module "Math". PROCEDURE MinuteTicks*; VAR x, y: ARRAY 60 OF REAL; s6, c6: REAL; i: INTEGER; BEGIN x[0]:= 0.0; y[0] := 1.0; s6 := 0.10452846; c6 := 0.99452189; FOR i := 1 TO 59 DO x[i] := x[i-1] * c6 + y[i-1] * s6; y[i] := y[i-1] * c6 - x[i-1] * s6; Out.RealFix(x[i], 10, 3); Out.RealFix(y[i], 10, 3); Out.Ln; (* debug output *) END END MinuteTicks; Jörg Am 19.02.19, 20:06 schrieb "Jörg" <joerg.straube@iaeth.ch>: PROCEDURE HourTicks(r: INTEGER); BEGIN s:= r DIV 2; c := r * 181 DIV 209; x[0] := 0; y[0] := r; x[1] := s; y[1] := c; x[2] := c; y[2] := s; x[3] := r; y[3] := 0; x[4] := c; y[4] := -s; x[5] := s; y[5] := -c; x[6] := 0; y[6] := -r; x[7] := -s; y[7] := -c; x[8] := -c; y[8] := -s; x[9] := -r; y[9] := 0; x[10] := -c; y[10] := s; x[11] := -s; y[11] := c; END HourTicks br Jörg > Am 19.02.2019 um 18:23 schrieb Tomas Kral <thomas.kral@email.cz>: > > On Tue, 19 Feb 2019 18:08:54 +0100 > Tomas Kral <thomas.kral@email.cz> wrote: > >> circle into 12 >> segments > > Looking at V4 `ClockElem' I see this > > PROCEDURE Init; > VAR i: INTEGER; > BEGIN i := 0; > WHILE i < 60 DO > sin[i] := Math.sin(2 * Math.pi / 60 * i); > cos[i] := Math.cos(2 * Math.pi / 60 * i); > INC(i) > END > END Init; > > I probably cannot do without trigonometry, right? > > -- > Tomas Kral <thomas.kral@email.cz> > -- > Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems > https://lists.inf.ethz.ch/mailman/listinfo/oberon
HourTicks MinuteTicks
Thank you. From the perspective of pure gear mechanics (swiss watchmakers secret :-), we have gear ratios 1:12 (hour / minute), 1:60 (minute / seconds). I do not need second ticks. I wonder if I could get close just by sensible divide. -- Tomas Kral <thomas.kral@email.cz>
On Wed, 20 Feb 2019 10:19:34 +0100 Tomas Kral <thomas.kral@email.cz> wrote:
I wonder if I could get close just by sensible divide.
Perhaps a false track. From hour to minute tick, I just need to divide each 30 deg segment into 5 smaller ones, right? -- Tomas Kral <thomas.kral@email.cz>
30° / 5 = 6°. That's the reason, why I called my variables in MinuteTicks "s6" and "c6" (short for sin 6° and cos 6°) Jörg Am 20.02.19, 11:13 schrieb "Oberon im Auftrag von Tomas Kral" <oberon-bounces@lists.inf.ethz.ch im Auftrag von thomas.kral@email.cz>: On Wed, 20 Feb 2019 10:19:34 +0100 Tomas Kral <thomas.kral@email.cz> wrote: > I wonder if I could get close just > by sensible divide. Perhaps a false track. From hour to minute tick, I just need to divide each 30 deg segment into 5 smaller ones, right? -- Tomas Kral <thomas.kral@email.cz> -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
On Tue, 19 Feb 2019 20:06:34 +0100 Jörg <joerg.straube@iaeth.ch> wrote:
s:= r DIV 2; c := r * 181 DIV 209; x[0] := 0; y[0] := r; x[1] := s; y[1] := c; x[2] := c; y[2] := s;
Aha, we start with one quadrant as the rest is the symetry. Quadrant = 90 / 3 = 30 deg. sin(30) = 0.5 first triangle, the other is cos(30). 181 / 209 big enough triangle with angle proximing to 30 deg. -- Tomas Kral <thomas.kral@email.cz>
cos 30° = sqrt(3)/2. Here the rational numbers approximating cos 30° better and better: 13 / 15 error = 0.06% 58 / 67 error = 0.04% 71 / 82 error = 0.02% 84 / 97 error = 460 x10-7 181 / 209 error = 33 x10-7 808 / 933 error = 18 x10-7 989 / 1142 error = 9 x10-7 1170 / 1351 error = 2 x10-7 br Jörg Am 20.02.19, 09:40 schrieb "Oberon im Auftrag von Tomas Kral" <oberon-bounces@lists.inf.ethz.ch im Auftrag von thomas.kral@email.cz>: On Tue, 19 Feb 2019 20:06:34 +0100 Jörg <joerg.straube@iaeth.ch> wrote: > s:= r DIV 2; c := r * 181 DIV 209; > x[0] := 0; y[0] := r; > x[1] := s; y[1] := c; > x[2] := c; y[2] := s; Aha, we start with one quadrant as the rest is the symetry. Quadrant = 90 / 3 = 30 deg. sin(30) = 0.5 first triangle, the other is cos(30). 181 / 209 big enough triangle with angle proximing to 30 deg. -- Tomas Kral <thomas.kral@email.cz> -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
Tomas Just for completeness: ☺ The formula c := (r * 181 + 104) DIV 209; [now with correct rounding] calculates the HourTicks absolutely correctly for all circles with radius “r” up to 780 pixels. The formula c := (r * 71 + 41) DIV 82; is correct for circles with radius “r” up to 140 pixels. For larger circles it’s good as well, but sometimes off by one pixel. br Jörg
Hello Tomas, Am 19.02.2019 um 18:23 schrieb Tomas Kral:
I probably cannot do without trigonometry, right?
sine and cosine of 30°/60°/90° are all nice values (0, 0.5, sqrt(0.75), 1), so you can hardcode the values. For the square root you can use an approximation if you want to avoid calculating it. Regards, Michael
participants (3)
-
Jörg -
Michael Schierl -
Tomas Kral