LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-21-2017, 11:45 AM   #1
ArcreVuch
LQ Newbie
 
Registered: Jul 2017
Location: West Czech
Distribution: RHEL and Centos
Posts: 5

Rep: Reputation: Disabled
CLI Program to do math transformation on a set of x,y coordinates


Hi all
I'm looking for a cli program that does traslations, rotations, stretches, ecc, given a list of 2D coordinates

E.g., given a file with the list of coords ( a square, in this case )
3,2
5,2
5,5
3,5

Ii need something that does
PROGRAM -i inputfile --traslate +4x,-2y --rotate 40deg ... and so on

"R" is a bit overkill

TIA

Arcre
 
Old 08-21-2017, 02:03 PM   #2
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237
Blog Entries: 2

Rep: Reputation: Disabled
expr is a built-in utility that lets you evaluate mathematical, logical, and scientific expressions, and nearly anything else. Its most commonly used in shell scripts though. The expr manpage isn't particularly helpful, so you'll need to search the internet to get help with it..

Last edited by justmy2cents; 08-22-2017 at 01:05 PM.
 
Old 08-21-2017, 02:27 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,617
Blog Entries: 11

Rep: Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655Reputation: 3655
Belated welcome to LQ!

Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

From the description you are doing simple coordinate transformations. The operations reduce to specifying the transformation function, the relationship between original and new coordinates, and should be mostly language agnostic.

An obvious candidate would be GNU bc. Simply write the mathematical transformation function(s) in bc's syntax, then pass it your original coordinates.

If you could work better with matrix notation or need a more complete solution, GNU Octave would be my tool of choice.

If you are up to some simple programming beyond the math itself, just choose a language you are familiar with and get started. The transformations you describe should not need anything beyond the built-in math capabilities of most programming languages. If you do need more complex functions, check available libs.

One caution with shell scripts though (thinking specifically about Bash), it supports only integer math operations natively. So if you need floating point precision you will need to invoke an external program, usually bc for me.

And yet another caution using expr as it has been already mentioned. From O'Reilly, Classic Shell Scripting:

Quote:
# Classic Shell Scripting - Oreilly, Section 7.6.3
# The expr command is one of the few Unix commands that is poorly designed and hard to use.
# Although standardized by POSIX, its use in new programs is strongly discouraged, since
# there are other programs and facilities that do a better job. In shell scripting, the major
# use of expr is for shell arithmetic, so that is what we focus on here. Read the expr(1)
# manpage if you're curious about the rest of what it can do.
#
# expr's syntax is picky: operands and operators must each be separate command-line arguments;
# thus liberal use of whitespace is highly recommended. Many of expr's operators are also
# shell metacharacters, so careful quoting is also required.
#
Use the shell built-ins instead!
 
1 members found this post helpful.
Old 08-22-2017, 04:19 AM   #4
ArcreVuch
LQ Newbie
 
Registered: Jul 2017
Location: West Czech
Distribution: RHEL and Centos
Posts: 5

Original Poster
Rep: Reputation: Disabled
I was actually looking for an already cooked pork chop but actually bc and some bash glue is enough to manage few thousands of records
Thanks

Arcre
 
Old 08-22-2017, 05:28 PM   #5
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 633

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
I'd also suggest python with numpy.
You could then combine this with argparse to make it a cli tool if needed.

Code:
import numpy as np

def transform(array, x=0, y=0):
    '''In place transformation of x, y coordinates'''
    array[:, 0] += x
    array[:, 1] += y 

my_array = np.loadtxt('coord', delimiter=',', dtype='intc')
Code:
array([[3, 2],
       [5, 2],
       [5, 5],
       [3, 5]], dtype=int32)
Code:
transform(my_array, x=-10)
array([[-7,  2],
       [-5,  2],
       [-5,  5],
       [-7,  5]], dtype=int32)
 
Old 08-22-2017, 06:02 PM   #6
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
You can do math in bash, but it doesn't do floating point. Although shift the decimal a few places and that doesn't matter much.

$ echo $(( $(( 10 * 6 )) / 3 ))

You could use bc to do more complex math with floating point natively. Mostly I fake shift the decimal and do bash math.

Like a couple $1.65 items with a quantity of 6 after an 8.5% tax.

Code:
$ FAKETAX=$(( 1000 + 85 )); FAKEQUANTITY=6; FAKEVALUE=$((100 + 65)); \
  FAKEFLOAT=$( echo $(( $(( $FAKETAX * $(( $FAKEQUANTITY * $FAKEVALUE )) )) / 1000 )) ); \
  FAKELENGTH=${#FAKEFLOAT}; FAKEOFFSET=$(( $FAKELENGTH - 2 )); \
  echo -n $(( $FAKEFLOAT / 100 )); echo -n "."; echo ${FAKEFLOAT:$FAKEOFFSET:2}
A bit of a hacky way to do math, but if you don't feel like mixing your poisons, an option.
 
Old 08-22-2017, 06:48 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,194

Rep: Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040Reputation: 1040
Rotation need sin() and cos() functions.

jlinkels
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Command xinput to set Coordinate Transformation Matrix thorkelljarl Linux - Software 1 04-24-2017 09:46 AM
math help in awk to pivot xy coordinates gary_in_springhill Programming 10 01-23-2011 12:52 PM
math program that I can enter math functions ... Four General 5 04-19-2006 08:02 PM
3D Graphing program for math course toaster.waffle Linux - Software 4 10-21-2005 10:10 PM
Math Program evian Linux - Software 4 09-09-2004 10:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:55 AM.

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