The time now is Sun 30 Aug 2015, 10:15
All times are UTC  4 
Author 
Message 
greengeek
Joined: 20 Jul 2010 Posts: 3218 Location: New Zealand

Posted: Thu 27 Aug 2015, 06:29 Post subject:
Polar equation: how to create coordinates with Awk? (Solved) 

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 coordinates and i am wondering if there is a way to do it with bash or similar.
Normal coordinates 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 coordinates 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 coordinates 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 coordinates 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 coordinates in Librecad they need to be expressed slightly differently (see here) and I need to have the coordinates 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 coordinates and would really like bash (or similar) to do the legwork and create the coordinate 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!
.

Back to top



SFR
Joined: 26 Oct 2011 Posts: 1311

Posted: Thu 27 Aug 2015, 07:12 Post subject:
Re: Polar equation  how to create coordinates ? 

greengeek wrote:  175.00<0
174.24<10
160.36<45
94.91<127 
Hey Greengeek
Bash itself doesn't support any trigonometric functions, but AWK does:
Code:  # 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
# 
Note that awk operates on radians, hence the conversion: RAD=(π*DEG)/180.
I hope that's what you wanted..?
[a bit later]
Quote:  I would like to plot 360 (or even 720) of these coordinates and would really like bash (or similar) to do the legwork and create the coordinate 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:  awk 'BEGIN {for (i=0; i<=359.5; i+=0.5) printf "%.2f<%.1f\n", 125+50*cos((3.14159265*i)/180), i}' 
Greetings!
_________________ [O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.

Back to top



some1
Joined: 17 Jan 2013 Posts: 48

Posted: Thu 27 Aug 2015, 09:06 Post subject:


SFR:Yes,just 1 awkcall.Nice

As an alternative to awk,check out bc.


Back to top



greengeek
Joined: 20 Jul 2010 Posts: 3218 Location: New Zealand

Posted: Thu 27 Aug 2015, 15:22 Post subject:
Re: Polar equation  how to create coordinates ? 

SFR wrote:  Code:  awk 'BEGIN {for (i=0; i<=359.5; i+=0.5) printf "%.2f<%.1f\n", 125+50*cos((3.14159265*i)/180), i}' 
Greetings!  Excellent. In a split second I have my 720 coordinates! Many thanks!
some1 wrote:  As an alternative to awk,check out bc.  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)
Code:  # 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 19911994, 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 19911994, 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


Back to top





You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum

Powered by phpBB © 2001, 2005 phpBB Group
