LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-16-2010, 05:51 AM   #1
gary_in_springhill
Member
 
Registered: Mar 2008
Posts: 136

Rep: Reputation: 21
complex math calculation in shell script needed


Here is my situation:
I have a series of .plt files that are geared for a device that has it's xy origin at the lower left of the paper however some devices use the center of the paper as it's origin in a piped shell script how can I convert this file to a center origin plot file?
Here are some knowns: the existing file always Lower Left 0.0
the existing file max xy is the page upper right x.x

the desired output file must have it's 0.0 at the exact center of page (page size will vary).

Ideas: Based on the sample files included maybe use PU and PD as the triggers for inputs to be calculated as any other commands have nothing to do with coordinates.
These files are exactly the same on a small letter (page size will vary on job) size page one is center origin and the other is LL origin.

Thanks for any help or advice....
Gary
Attached Files
File Type: txt origin-center.plt.txt (2.0 KB, 21 views)
File Type: txt origin-LL.plt.txt (2.1 KB, 20 views)
 
Old 02-16-2010, 06:31 AM   #2
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
Maybe you should have a look at awk, which is much better in doing math than plain shell. You can call awk from your shell script. Since you do not know the maximum coordinate values in advance, you will have to store the input data in some way, because the maximum coordinate values can change with each new input record, invalidating the results of the former conversion. So you will need at least two processing steps, when the coordinate values of the corners are unknown. But in principle this task awk should be able to do the task.
 
Old 02-16-2010, 06:48 AM   #3
gary_in_springhill
Member
 
Registered: Mar 2008
Posts: 136

Original Poster
Rep: Reputation: 21
math skills

I figured awk would do it but my math skills and ability to grasp calculations are limited (very limited)
Anyone out there feel like having a go at it?
 
Old 02-16-2010, 07:14 AM   #4
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
I think you should try the following algorithm:
let awk match "PD<x> <y>" and "PU<x> <y>" and do the following:
x -= (maxx + minx)/2
y -= (maxy + miny)/2

write out the new record.

I'll try to write a sample awk script ...

Last edited by irmin; 02-16-2010 at 07:49 AM.
 
Old 02-16-2010, 08:09 AM   #5
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
I have two awk scripts appended to this post.

The first one will determine the dimensions of your plot:
Code:
awk -f minmax.awk yourplot.plt
The second one will move the center to (0,0):
Code:
awk -v minx=... -v maxx=... -v miny=... -v maxy=... -f conv.awk yourplot.plt > yournewplot.plt
In any case you will need to either know the dimensions of your plot or save your plot and determine them.
Attached Files
File Type: txt conv.awk.txt (254 Bytes, 10 views)
File Type: txt minmax.awk.txt (412 Bytes, 11 views)
 
Old 02-16-2010, 09:07 AM   #6
gary_in_springhill
Member
 
Registered: Mar 2008
Posts: 136

Original Poster
Rep: Reputation: 21
great!

OK if I have a known lower left origin file saved as temp.plt can I just cat temp.plt |minmax.awk |conv.awk > ceter-origin.plt

or

cat temp.plt |minmax.awk > temp.plt ; cat temp.plt|conv.awk > ceter-origin.plt
 
Old 02-16-2010, 09:17 AM   #7
gary_in_springhill
Member
 
Registered: Mar 2008
Posts: 136

Original Poster
Rep: Reputation: 21
plotting device

I failed to mention the plotting device scans the paper size when loading paper so it knows what size paper is being used and therefore knows where the center 0.0 is based on it's scan.
 
Old 02-16-2010, 09:45 AM   #8
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
If you know the dimensions of the paper, then you can simply do the following:
Code:
 cat temp.plt | awk -v minx=0 -v miny=0 -v maxx=width-1 -v maxy=height-1 -f conv.awk > output.plt
This will move the specified box such, that its center is at the origin. In this case you do not need the minmax awk script and there is no need to temporarily store the input data.

In detail: If the origin is at the lower-left corner (0,0) and the width and height is (10000,7500), then the script will recalculate the coordinates such that the lower-left corner will be at (-5000,-3750) and the upper right one at (5000,3750).
 
Old 02-16-2010, 10:00 AM   #9
gary_in_springhill
Member
 
Registered: Mar 2008
Posts: 136

Original Poster
Rep: Reputation: 21
Problem

The problem is the plotter knows the paper size in plotter units and where center 0,0 is but there is no way for the script to get these variables from the plotter. I'm unsure of why the paper size needs to be known at all. With coordinates being sent out in positive and negative xy's moving out from the starting point center 0,0 because the plotter knows where 0,0 is from it's paper scan. I must be missing something ...
If minmax is needed how would my cat temp.plt | xxx | xxx appear?
 
Old 02-16-2010, 10:15 AM   #10
gary_in_springhill
Member
 
Registered: Mar 2008
Posts: 136

Original Poster
Rep: Reputation: 21
maybe a solution?

The original plt file is derived from a .ps or .pdf file can we sed/grep the "%%BoundingBox: 0 0 566 396" line before it gets processed into a plot file?

http://amath.colorado.edu/documentat...ence/bbox.html
 
Old 02-16-2010, 10:17 AM   #11
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
Actually you do not need the paper size.

Take the modified version of minmax.awk and use the command:
Code:
awk -f <(echo "BEGIN { "; awk -f minmax.awk input.plt; echo "}"; cat conv.awk) input.plt > output.plt
Attached Files
File Type: txt minmax.awk.txt (404 Bytes, 13 views)
 
Old 02-16-2010, 10:28 AM   #12
gary_in_springhill
Member
 
Registered: Mar 2008
Posts: 136

Original Poster
Rep: Reputation: 21
plotter units

plotter units = 1016 per inch
 
Old 02-16-2010, 10:36 AM   #13
gary_in_springhill
Member
 
Registered: Mar 2008
Posts: 136

Original Poster
Rep: Reputation: 21
files are same

awk -f <(echo "BEGIN { "; awk -f minmax.awk hpgl.plt; echo "}"; cat conv.awk) hpgl.plt > output.plt

yields identical original files.
no error though
 
Old 02-16-2010, 10:41 AM   #14
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
What is the output of
Code:
awk -f minmax.awk hpgl.plt
awk -f minmax.awk output.plt
?

If the files are not altered, they are already centered.
 
Old 02-16-2010, 10:48 AM   #15
gary_in_springhill
Member
 
Registered: Mar 2008
Posts: 136

Original Poster
Rep: Reputation: 21
output

both files read
minx=200000
maxx=-200000
miny=200000
maxy=-200000

but the output files xy cordinates should have some negative x and y cordinates due to the fact that from 0.0 being in the center of the page in order to plot down in the bottom left quadrant the xy values for that quadrant will be negative values. Without negatives you are limited to the upper left quadrant only.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to do simple math in shell script gidrow Programming 5 07-30-2009 03:46 PM
csh Shell Script: Getting wc command output to variable to do math on it, how? vxc69 Programming 5 05-04-2009 04:31 PM
improve math calculation... java os2 Programming 1 10-21-2004 06:17 PM
putting a math calculation in a cron job the_rhino Linux - Newbie 3 10-11-2004 01:46 PM
c math calculation alaios Programming 3 06-01-2004 01:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration