Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sat 29 Aug 2015, 23:46
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Polar equation: how to create coordinates with Awk? (Solved)
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [4 Posts]  
Author Message
greengeek

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

PostPosted: 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 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 Smile
(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
View user's profile Send private message 
SFR


Joined: 26 Oct 2011
Posts: 1311

PostPosted: Thu 27 Aug 2015, 07:12    Post subject: Re: Polar equation - how to create co-ordinates ?  

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 co-ordinates and would really like bash (or similar) to do the legwork and create the co-ordinate list for me Smile
(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
View user's profile Send private message 
some1

Joined: 17 Jan 2013
Posts: 48

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

SFR:Yes,just 1 awk-call.Nice
---
As an alternative to awk,check out bc.
--
Back to top
View user's profile Send private message 
greengeek

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

PostPosted: Thu 27 Aug 2015, 15:22    Post subject: Re: Polar equation - how to create co-ordinates ?  

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 co-ordinates! 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 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
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [4 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

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
[ Time: 0.0970s ][ Queries: 11 (0.0122s) ][ GZIP on ]