I am wondering if it is possible to use bash or similar to help calculate points on a graph.
I have a polar equation that describes a shape that is almost a circle but not quite. (The actual shape is a function of the movement of a mechanical device and I have calculated the polar equation as an accurate means of describing the shape).
I need to use a CAD programme to display this shape but I am using Librecad and I cant yet work out how to make the programme display the shape automatically. I have figured out how to enter "waypoints" (nodes?) manually so that I can get a reasonable approximation of the shape and this will be sufficient for my visual needs at present.
However, it is time consuming to manually calculate these co-ordinates and i am wondering if there is a way to do it with bash or similar.
Normal co-ordinates on an x,y (cartesian) graph would have a form such as (3,7) which would indicate a point 3 units across and 7 units up from the origin (0,0). However, in a polar equation the co-ordinates are expressed differently - they describe the location of the point by defining the distance of the point from the origin, followed by the angle from horizontal. The coordinates look similar eg (12,70) but the numbers mean different things.
There is a good comparison of cartesian versus polar co-ordinates here
My polar equation is:
r=125+50cosQ
where r is the distance of the waypoint (node) from the origin
Q is the angle from horizontal
I use this equation to plot points by doing the following:
1) Specify the angle and find the cosine of it.
2) Multiply this cosine by 50
3) Add 125 to the result (which will give me the length of r in mm)
This results in co-ordinates such as:
For 0 degrees: (175.00,0)
For 10 degrees (174.24,10)
For 45 degrees (160.36,45)
For 127 degrees (94.91,127)
and so on
(I think I need to retain 2 decimal places in the "length" field).
When I use these co-ordinates in Librecad they need to be expressed slightly differently (see here) and I need to have the co-ordinates in the following form to make them easy to enter manually into Librecad:
175.00<0
174.24<10
160.36<45
94.91<127
and so on.
I would like to plot 360 (or even 720) of these co-ordinates and would really like bash (or similar) to do the legwork and create the co-ordinate list for me
(Ideally I need values for 0 degrees, 0.5, 1, 1.5, 2, 2.5 etc etc right up to 359.5 degrees)
Any suggestions gratefully received!
.
Polar equation: how to create coordinates with Awk? (Solved)
Re: Polar equation - how to create co-ordinates ?
Hey Greengeekgreengeek wrote:175.00<0
174.24<10
160.36<45
94.91<127
Bash itself doesn't support any trigonometric functions, but AWK does:
Code: Select all
# awk 'BEGIN { X=0; printf "%.2f<%d\n", 125+50*cos((3.14159265*X)/180), X }'
175.00<0
# awk 'BEGIN { X=10; printf "%.2f<%d\n", 125+50*cos((3.14159265*X)/180), X }'
174.24<10
# awk 'BEGIN { X=45; printf "%.2f<%d\n", 125+50*cos((3.14159265*X)/180), X }'
160.36<45
# awk 'BEGIN { X=127; printf "%.2f<%d\n", 125+50*cos((3.14159265*X)/180), X }'
94.91<127
#
I hope that's what you wanted..?
[a bit later]
I would like to plot 360 (or even 720) of these co-ordinates and would really like bash (or similar) to do the legwork and create the co-ordinate list for me
(Ideally I need values for 0 degrees, 0.5, 1, 1.5, 2, 2.5 etc etc right up to 359.5 degrees)
Code: Select all
awk 'BEGIN {for (i=0; i<=359.5; i+=0.5) printf "%.2f<%.1f\n", 125+50*cos((3.14159265*i)/180), i}'
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]
[b][color=green]Omnia mea mecum porto.[/color][/b]
Re: Polar equation - how to create co-ordinates ?
Excellent. In a split second I have my 720 co-ordinates! Many thanks!SFR wrote:Greetings!Code: Select all
awk 'BEGIN {for (i=0; i<=359.5; i+=0.5) printf "%.2f<%.1f\n", 125+50*cos((3.14159265*i)/180), i}'
Thanks - I had not heard of that before. Just gave it a try and I struggled with the syntax, but it seems you run bc, then enter the equation on the next line (not directly following the bc command)some1 wrote:As an alternative to awk,check out bc.
Code: Select all
# bc 1+2
File 1+2 is unavailable.
# bc (1+2)
bash: syntax error near unexpected token `1+2'
# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
sqrt (9)
3
(interrupt) Exiting bc.
# bc sqrt(9)
bash: syntax error near unexpected token `('
# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1+2
3
3*3
9
- puppy_apprentice
- Posts: 299
- Joined: Tue 07 Feb 2012, 20:32