r/dailyprogrammer • u/Elite6809 1 1 • Sep 01 '14
[9/01/2014] Challenge #178 [Easy] Transformers: Matrices in Disguise, pt. 1
(Easy): Transformers: Matrices in Disguise, pt. 1
Or, rather, transformations. Today we'll be doing a bit of basic geometry. We'll be writing a program which will take a point in 2-dimensional space, represented as (X, Y) (where X and Y can be decimal and negative), transform them a number of times in different ways and then find the final position of the point.
Your program must be able to do the following:
Translations - ie. offsetting the X and Y co-ordinates by a given amount http://i.imgur.com/3jI4sGI.png
Rotations by an arbitrary angle around a given point http://i.imgur.com/9c0ji7c.png
Scale relative to a point http://i.imgur.com/vHUfXv2.png
Reflection over the X or Y axis http://i.imgur.com/X6JH6pT.png
Formal Inputs & Outputs
Input
You will take an starting point (X, Y), such as:
(3, 4)
On new lines, you will then take commands in the format:
translate(A, B) - translate by (A, B)
rotate(A, B, C) - rotate around (A, B) by angle C (in radians) clockwise
scale(A, B, C) - scale relative to (A, B) with scale-factor C
reflect(axis) - reflect over the given axis
finish() - end input and print the modified location
Where axis is one of X or Y.
Output
Print the final value of (X, Y) in the format:
(2.5, -0.666666)
Test Case
Test Case Input
(0, 5)
translate(3, 2)
scale(1,3,0.5)
rotate(3,2,1.57079632679)
reflect(X)
translate(2,-1)
scale(0,0,-0.25)
rotate(1,-3,3.14159265359)
reflect(Y)
Test Case Output
(-4, -7)
Notes
I want to say two things. First, this may be a good opportunity to learn your language's 2-D drawing capabilities - every time a command is given, represent it on an image like I have done with the examples, so you can see the path the co-ordinate has taken. Secondly, this is a multi-part challenge. I'm not sure how many parts there will be, however it may be a good idea to prepare for more possible commands (or, if you're crazy enough to use Prolog - you know who you are - write an EBNF parser like last time, lol.) If you know how, it would be clever to start using matrices for transformations now rather than later.
5
u/XenophonOfAthens 2 1 Sep 01 '14 edited Sep 01 '14
I've never been called out in a problem description before! I was going to do this in Python, but screw you, /u/Elite6809, I'm doing this shit in Prolog!
Fortunately, for this problem no actual parsing is needed, it's perfectly sufficient to use read_term, which just reads in a prolog term from stdin and then do stuff to it.
As the problem description suggested, I'm using matrix transformations to do all the work instead of operating on the points directly. It's been a long time since I've done that, I really hope I haven't screwed anything up. Seems to work fine though. It makes the operations more elegant, just matrix multiplication using specific matrices.
Example output, with added comments:
Edit: Edited the code to change rotations to clockwise instead of counter-clockwise, and fixed a bug with reflection (it was reflecting the wrong axis).
7
u/skeeto -9 8 Sep 01 '14 edited Sep 01 '14
C. Commands are executed using a static table that maps operation names to function pointers. The function pointer
opuses an empty parameter list(). In C this means the parameters are unspecified, not that the function takes no arguments! This allows me to make these functions accept differing numbers of arguments. This is different from declaring the parameter list(void), which you will sometimes see in C. That declares that the function explicitly doesn't accept any arguments.Edit: reddit's having a lot of 504 problems today and this took me a dozen or so tries to submit.
4
u/Busybyeski Sep 02 '14 edited Sep 02 '14
Python
Howdy! First /r/dailyprogrammer challenge. Would love feedback! I made each function access the global x/y coordinates of the point and only used the math library's arctan, sine, and cosine for rotation.
I didn't catch the matrices hint, and I'm not sure how well I've prepared myself for the future parts of this problem, but we'll see. This passes the (0, 5) to (-4, -7) test listed.
3
u/hutsboR 3 0 Sep 01 '14 edited Sep 02 '14
Dart:
Usage:
EDIT: Okay, scale() seems to be working correctly.
Output:
Graph image, (6, 4) scaled relative to (2, 4) with a scale-factor of 1.5
3
u/qZeta Sep 01 '14 edited Sep 02 '14
Haskell
This has been some fun. The parser is probably a little bit too much, but it was the first thing that came into my mind. Also, the
OPcould get another constructorFun (Point -> Point), but that would prevent bothEqandShow, which I wanted to keep (at least for this part).Edit: Accidentally mirrored along the wrong axis.
3
u/Coplate Sep 02 '14 edited Sep 02 '14
Sweet sexy C, using the ever safe 'gets'
And output showing the correct answer:
2
u/Godspiral 3 3 Sep 01 '14 edited Sep 01 '14
could you provide sample input including C param for rotations and scale, along with output for sample?
/u/XenophonOfAthens provides a link to wolfram alpha
2
u/complxor Sep 01 '14
Javascript
Any feedback is appreciated
2
u/possiblywrong Sep 02 '14
In Python using Numpy, with commands expressed as 3x3 transformation matrices in homogeneous coordinates (so that composition=multiplication). This is slightly longer than it needs to be-- the rotate(), scale(), and reflect() seemed slightly yucky to me, since (1) the sense of the rotation is backward, and (2) they can be expressed in terms of conjugations with simpler origin-relative translations, rotations, and scaling.
3
u/Godspiral 3 3 Sep 01 '14 edited Sep 01 '14
translate =: +
rotate =: 1 : (' [: +/"1 ((2 2 $ 2&o. , -@:(1&o.) , 1&o. , 2&o.) m ) * 2 2 $ -~')
scale =: 1 : '[ + m * |@:-~'
NB. reflect takes as left param 0 1 2 3 -> 0 x y both
reflect =: ] * (1 1 , _1 1 , 1 _1 ,: _1 _1 ) {~ [
0 0 (1 reflect [ 2 scale [ 2 rotate translate) 3 4
_9.77126 2.12661
1 1 (1 reflect [ 2 scale [ 2 rotate translate) 3 4
_12.7713 1.12661
1
u/[deleted] Sep 02 '14 edited Sep 02 '14
My attempt in Ruby.
For some reason I keep getting (-2.5, -6.5) instead of (-4, -7) -- any ideas?Fixed it! See the rotate method.1
u/spfy Sep 02 '14 edited Sep 02 '14
Java. I can't seem to get the correct answer for the test input. Would anyone be willing to check out my rotate method? I'm pretty sure that's the culprit. I subtract the rotation point from the target, multiply a rotation matrix, and add the rotation point back. It should be working, right? ):
EDIT: Actually, I ran some tests on wolfram alpha and my rotate method seems to work fine. IDK.
EDIT 2: Okay, I fixed it. It seems wolfram alpha and I are confused about the direction of rotation. Had to make the radians negative first and I had to swap my rotation matrices. I get the correct answer now.
Here's my main class for anyone who wants to test mine themselves:
1
u/fvandepitte 0 0 Sep 02 '14 edited Sep 03 '14
I've found my error, it was in the rotate function here is my new and improved version: Still, if anyone could give me some pointers just let me know
Main.CPP
Point.h
Point.cpp
Result:
1
u/[deleted] Sep 02 '14 edited Sep 02 '14
This is my first submission to this subreddit. The solution is written in Python and I've only just recently started, any feedback at all is appreciated.This code asks the user for commands and other inputs. This passes the test input.
1
u/ff123 Sep 02 '14
Rust. A day late, but I had to take the time too look up the syntax since this is my first time playing with it.
1
u/bike-curious Sep 02 '14
Go lang. scale/rotate seems to have a bug but I'm too sleepy to debug it atm :)
1
u/egportal2002 Sep 03 '14 edited Sep 03 '14
gawk:
yielding:
1
u/MaximaxII Sep 04 '14
A very succinct Python solution. It uses eval(), so there's the added benefit of having certain extra functionality, namely:
exit()for a clean exit.mathsuite (math.pi, math.sin(), math.cos()...).count(), abs(), len()...)#Challenge #178 Easy - Python 3.4
1
u/[deleted] Sep 04 '14
Made this in Ruby. I had to copy some equations because well, I didn't remember them. 😛
I also didn't really comply with the "spec". But ah, whatever. Yes, it passes the test case.
1
u/satanic_warhamster Sep 06 '14
Python 2.7
I'm new to Python and this is my first submission to this subreddit, so feel free to critique me(Though I doubt anyone will seeing that I'm submitting 4 days after this thread was created).
Also, I'd be lying If I said I didn't partly refer to /u/duncan3126's code. So big thanks to him
Input:
Enter value for X Axis 0 Enter value for Y axis 5
Output:
(-4.0, -7.0)
EDIT: Formatting
1
u/datgohan Sep 11 '14
Written this in Python as I've not written any Python before so comments and advice are very welcome as I want to improve. I think I've messed up in the maths somewhere and I'm still trying to find the issue but I'm happy that the flow of the program works (took me ages!)
1
u/Noupoi Sep 01 '14 edited Sep 01 '14
Java. I added support for putting in decimals using Regex - might have been slightly overkill. I realise my code could be slightly tidier and probably far more compact. Affine transformations are something I'll do next time, should I revisit this.
Edit: made sure lower and upper case both work. Seems like something's broken, given the test case provided. I'll look into this
1
u/Joris1225 Sep 02 '14
My implementation in Java. It correctly outputs (-4.0,-7.0) for the test input.
And the main():
0
u/[deleted] Sep 01 '14
[deleted] — view deleted comment