### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [SOLVED] Generating a scale factor from an angle  (Read 1252 times)

#### MarkMLl

• Hero Member
• Posts: 7503
##### [SOLVED] Generating a scale factor from an angle
« on: August 04, 2024, 09:51:06 pm »
I'm sorry, but I'm having a severe case of brain fade here: I've been focusing on comms-related stuff for so long that my geometry's been swapped out to some extremely slow tertiary-memory device and the operators haven't yet found the cartridge in the vault.

Related to https://forum.lazarus.freepascal.org/index.php/topic,67908.msg523888.html#msg523888, I'm trying to work out a function with these characteristics:

* If the input is 0 (degrees) then the output is 1.

* If the input is +90 then the output is Sqrt(2).

* For other +ve inputs, the output lies on a roughly Log10 curve such that beyond +90 it gradually flattens out somewhat larger than Sqrt(2) (let's say no larger than 2, but this is a relatively unimportant detail).

* If the input is -90 then the output is 1 /  Sqrt(2).

* For other -ve inputs, the output lies on a roughly Log10 curve such that beyond -90 it gradually flattens out somewhat smaller than 1 / Sqrt(2) (let's say no smaller than 1/2, but this is a relatively unimportant detail).

It would be nice if gross discontinuities could be avoided, but I don't think there's a single smooth function that will do this.

What I'm trying to do is take one or more airfield circuit patterns, and convert them to a "planes nearby" area where tightly-reentrant corners (-ve angles) don't close up completely.

Any polite suggestions would be appreciated :-)

MarkMLl
« Last Edit: August 05, 2024, 02:10:45 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### Curt Carpenter

• Hero Member
• Posts: 505
##### Re: Generating a scale factor from an angle
« Reply #1 on: August 04, 2024, 10:21:15 pm »
Maybe try a curve-fitting algorithm or something like https://mycurvefit.com/ ?  First step it seems to me would be to graph what you're looking for with a few more data points.

#### jamie

• Hero Member
• Posts: 6529
##### Re: Generating a scale factor from an angle
« Reply #2 on: August 05, 2024, 12:01:51 am »
are you sure it isn't +/- 1.414 on the output?

In any case I have what I understand in your description.

Code: Pascal  [Select][+][-]
1. Function Yup(Deg:Double):Double;
2. Begin
3.
4.   Result := 1 + Sin(DegToRad(abs(Deg)))*0.414;
5.   If Deg <0 Then
6.      result :=  1/Result;
7. end;
8.
9.

That does 0n 0..90D = 1..1.414

and on the -0..90   1.0 .. 0.707;
The only true wisdom is knowing you know nothing

#### MarkMLl

• Hero Member
• Posts: 7503
##### Re: Generating a scale factor from an angle
« Reply #3 on: August 05, 2024, 09:01:37 am »
are you sure it isn't +/- 1.414 on the output?

Absolutely sure. The result is to be +ve, Sqrt(2) at +90 levelling off, 1 / Sqrt(2) for -90 levelling off... note my specification of a log.

Thanks for the suggestion, I'll take a look.

Slightly later:

Code: Pascal  [Select][+][-]
1.   function scaleLengthAtCorner2(const deviation: TangleRads): double;
2.
3.   begin
4.     Result := 1 + Sin(deviation)*0.414;
5.     If Deviation <0 Then
6.      result :=  1/Result
7.   end { scaleLengthAtCorner2 } ;
8.
9.
10.
11.   procedure testScaleLengthAtCorner;
12.
13.   var
14.     i: integer;
15.
16.   begin
17.     for i := -12 to 12 do
18.       WriteLn((i * 10):4, ' ', scaleLengthAtCorner2((i * 10) / (180/Pi)):6:3);
19.     halt
20.   end { testScaleLengthAtCorner } ;
21.

Code: [Select]
-120  1.559
-110  1.637
-100  1.688
-90  1.706
-80  1.688
-70  1.637
-60  1.559
-50  1.464
-40  1.363
-30  1.261
-20  1.165
-10  1.077
0  1.000
10  1.072
20  1.142
30  1.207
40  1.266
50  1.317
60  1.359
70  1.389
80  1.408
90  1.414
100  1.408
110  1.389
120  1.359

So the 0 and +90 points are right...

MarkMLl
« Last Edit: August 05, 2024, 10:02:43 am by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### MarkMLl

• Hero Member
• Posts: 7503
##### Re: Generating a scale factor from an angle
« Reply #4 on: August 05, 2024, 11:15:54 am »
Maybe try a curve-fitting algorithm or something like https://mycurvefit.com/ ?  First step it seems to me would be to graph what you're looking for with a few more data points.

Ah yes, that's nice. I was so hung up on getting something correct using Sin and Log that I'd not really thought about curve fitting.

That site looks useful, and allows me to put off the Gnu Octave learning curve for a while longer.

Code: Pascal  [Select][+][-]
1.   function scaleLengthAtCorner3(const deviation: TangleRads): double;
2.
3.   (* This from https://mycurvefit.com/ (thanks, Curt) using example data        *)
4.   (*                                                                            *)
5.   (* -2.09      0.66    (-120 degs)                                             *)
6.   (* -1.57      0.707   (-90)                                                   *)
7.   (* 0          1       (0)                                                     *)
8.   (* 1.57       1.414   (+90)                                                   *)
9.   (* 2.09       1.5     (+120)                                                  *)
10.
11.   begin
12.     result := 1.00788 + (0.2096875 * deviation) + (0.01767879 * Sqr(deviation))
13.   end { scaleLengthAtCorner3 } ;
14.
15.
16.   procedure testScaleLengthAtCorner;
17.
18.   var
19.     i: integer;
20.
21.   begin
22.     for i := -12 to 12 do
23.       WriteLn((i * 10):4, ' ', scaleLengthAtCorner3((i * 10) / (180/Pi)):6:3);
24.     halt
25.   end { testScaleLengthAtCorner } ;
26.

Code: [Select]
-120  0.646
-110  0.670
-100  0.696
-90  0.722
-80  0.750
-70  0.778
-60  0.808
-50  0.838
-40  0.870
-30  0.903
-20  0.937
-10  0.972
0  1.008
10  1.045
20  1.083
30  1.123
40  1.163
50  1.204
60  1.247
70  1.290
80  1.335
90  1.381
100  1.428
110  1.476
120  1.525

MarkMLl
« Last Edit: August 05, 2024, 11:33:57 am by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### Jonax

• New Member
• Posts: 17
##### Re: [SOLVED] Generating a scale factor from an angle
« Reply #5 on: August 05, 2024, 10:03:51 pm »
I'm a bit late to the party. I too had a misplaced cartridge which was not easily retreived.

I'm glad you already got a suitable solution. I can add the alternative to use the exponential function
which  is continuous and seems to fit your desired check points, after a little tweaking.

Should work for angle input in degrees between -180 and 180

The constant cK= 0.00385081766977747394120684511921; // ln(2)/180

output:=exp(cK*input);

Hope this can be helpful

#### MarkMLl

• Hero Member
• Posts: 7503
##### Re: [SOLVED] Generating a scale factor from an angle
« Reply #6 on: August 05, 2024, 10:10:24 pm »
Thanks, I'll take a look at that but not necessarily immediately.

I've been hacking at these geometry problems for at least a week- not full time, I'm glad to say. It's not exactly application-critical, but on the balance worth doing since the fewer times a user has to enter approximately the same dats (probably in a .ini file) the better.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### Jonax

• New Member
• Posts: 17
##### Re: [SOLVED] Generating a scale factor from an angle
« Reply #7 on: August 05, 2024, 10:35:40 pm »

#### Jonax

• New Member
• Posts: 17
##### Re: [SOLVED] Generating a scale factor from an angle
« Reply #8 on: August 10, 2024, 01:32:33 pm »
I did some futile hacking too. The polynom approach seemed promising at first but turned out to be a dead end.

Here the results from the exponential approach.

output:=exp(cK*input);

My formatting s*cks but the math is solid.
Fits all five checkpoints

-180 0.5
-170 0.519
-160 0.540
-150 0.561
-140 0.583
-130 0.606
-120 0.629
-110 0.654
-100 0.680
-90 0.707
-80 0.734
-70 0.763
-60 0.793
-50 0.824
-40 0.857
-30 0.890
-20 0.925
-10 0.962
0 1
10 1.039
20 1.080
30 1.122
40 1.166
50 1.212
60 1.259
70 1.309
80 1.360
90 1.414
100 1.469
110 1.527
120 1.587
130 1.649
140 1.714
150 1.781
160 1.851
170 1.924
180 2

#### MarkMLl

• Hero Member
• Posts: 7503
##### Re: [SOLVED] Generating a scale factor from an angle
« Reply #9 on: August 13, 2024, 09:59:24 am »
I can confirm that's bang on, and I'd add that it illustrates how helpful a good command of maths theory can be :-)

It reminds me of an exchange in a computer mag. quite some years ago. Somebody referenced a "brain teaser" problem that had been posted elsewhere shortly before, saying that he'd worked out that it would take him several days of BASIC runtime to solve and asking whether he would be better off using a different language. Somebody responded by pointing out that he would, in fact, be better off having a good grasp of number theory, and proceeded to solve the problem in about ten lines of well-reasoned induction rather than by attempting to brute-force it.

So once again, many thanks to everybody who's helped with this: I'm now somewhat back on track and can turn my attention back to data collection and processing issues.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### Jonax

• New Member
• Posts: 17
##### Re: [SOLVED] Generating a scale factor from an angle
« Reply #10 on: August 14, 2024, 04:24:52 pm »
Glad to be of help and good to have had an opportunity to practice some maths. Am a bit rusty nowaways.
I guess many of us are. Indeed satisfying if the brain teasers can be solved by clever reasoning rather than brute force.

Though I must admit I mostly brute force problems. This was more of a stroke of luck where the solution turned out to be a neat well behaved function.

Good luck with the continued project, whatever that is. I'll return to my programming too.

Jonax