Results 1 to 13 of 13

Thread: Converting between coordinate systems

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2012
    Posts
    118

    Converting between coordinate systems

    Hi all,

    I mistakenly placed this in the .net forum originally.

    My maths suck rather a lot, but I have a problem that I need to solve in code... it relates to the real world.

    I have an X,Y coordinate system, in millimeters, that we may say is not rotated. The origin is in the center. Positive values are Up and Right.

    I have another system, in millimeters, that we will say is rotated with respect to the first, but cannot be known before calibration.
    This system has the same x-axis direction, but the Y axis is inverted (positive values down).

    I can lay one over the other and calibrate them... (literally, in the real world)... and for any number of points necessary, I can know the coordinate location on each system.

    Now I am looking for a methodology to take this calibration... and be able to know any point on the second system, when I know only the coordinates on the first.

    Example: for point -123345, 150 : what coordinate is that on the second system?

    Thank you all so much for your kind help.

    R

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Converting between coordinate systems

    How do you calibrate them?
    If calibration required a scaling and rotation, perhaps skewing, that could be entered into a Matrix, then you can use the Matrix (or the inverse matrix), to go from one coordinate system to the other.

    Really need to what your frame of reference is, and what does calibration do, calibrate to what?

  3. #3
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Converting between coordinate systems

    Posting a full example calculation will probably provide enough details for us to describe your situation rigorously and work out the conversion formula. Otherwise it's too vague to really say. As Passel suggests, the answer is probably simple to express with a matrix.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2012
    Posts
    118

    Re: Converting between coordinate systems

    Quote Originally Posted by passel View Post
    How do you calibrate them?

    Really need to what your frame of reference is, and what does calibration do, calibrate to what?
    The calibration is knowing the exact point locations of a single point, mapped to both systems.

    Ex: Point 1 = 9,7 on system 1 and 6.5,11.2 on system 2.
    Point 2 = 41,27 on system 1 and 43.2, 21.4 on system 2.

    These coordinate systems *are* frames of reference (literally). Two points are all you need to understand their relation.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2012
    Posts
    118

    Re: Converting between coordinate systems

    Quote Originally Posted by jemidiah View Post
    Posting a full example calculation will probably provide enough details for us to describe your situation rigorously and work out the conversion formula. Otherwise it's too vague to really say. As Passel suggests, the answer is probably simple to express with a matrix.

    Right, I assume that matrix multiplication will be involved.

    I can't post a "full example calculation", because that is, in essence, why I am here. If I knew how to do that, I'd already have my answer

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Converting between coordinate systems

    Quote Originally Posted by RyderS View Post
    The calibration is knowing the exact point locations of a single point, mapped to both systems.

    Ex: Point 1 = 9,7 on system 1 and 6.5,11.2 on system 2.
    Point 2 = 41,27 on system 1 and 43.2, 21.4 on system 2.

    These coordinate systems *are* frames of reference (literally). Two points are all you need to understand their relation.
    Well those example points seem to contradict what you said in your first post.
    This system has the same x-axis direction, but the Y axis is inverted (positive values down).
    The deltaX between point1 and point2 on system 1 is positive (goes from 9 to 41), and is also positive on System 2 (goes from 6.5 to 43.2).
    The deltaY between point1 and point2 on system 1 is positive (goes from 7 to 27), and is also positive on System 2 (goes from 11.2 to 21.4).

    So, if the Y coordinate system is inverted in one system, then your image must be projected upside down before the coordinate overlay is applied so that the Y direction appears to be the same in both systems.

    I think you probably just made those points up.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2012
    Posts
    118

    Re: Converting between coordinate systems

    Hi passel,

    You are correct... I failed to invert the y axis... but all values are possible... negative or positive. The xy space is infinite in all directions (but for practical use, within the bounds of long variables). Good catch!

    The points are not made up... but do represent two charts that share the same axis orientation.

    Correcting for the inverted axis on system 1 are Point 1: 9,28 Point 2: 41,8

    Thanks for your kind help.

    R

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Converting between coordinate systems

    Well, I'm not the best at math either, but it seems like this is just a scale to scale conversion, like Fahrenheit to Celsius, but in two directions X and Y.
    I could be wrong, but given your example coordinates, this seems like a logical progression toward the answer.

    Since you have two points in common, we can treat the first common point as a 0,0 coordinate in a third coordinate system that can be used to transition between the other two.
    {This is off the top of my head, and not based on actual knowledge of this being an approach).
    So, to translate the first chart to a new coordinate system system with 0,0 based at our first point, we would subtract the coordinates of the first point (9,7)
    1X1 = 9
    1Y1 = 7
    1nX1 = 0 'new X1 = 9 - 9
    1nY1 = 0 'new Y1 = 7 - 7

    and our second point (41,27) becomes (41 - 9, 27 - 7)
    1nX2 = 32
    1nY2 = 20
    So, we have a new coordinate system, the same scale, but with 0,0 located at the first reference point.

    For system 2, do the same thing
    (6.5,11.2)
    2X1 = 6.5
    2Y1 = 11.2
    2nX1 = 0 'new X1 = 6.5 - 6.5
    2nY1 = 0 'new Y1 = 11.2 - 11.2

    and our second point (43.2, 21.4) becomes (43.2 - 6.5, 21.4 - 11.2)
    2nX2 = 36.7
    2nY2 = 10.2

    So, the end result at this point is two scales that start at the same point (0,0) and we can compute the change in X and Y in both scales to see how the difference in X and Y scaling between the two coordinate systems.
    dx = 2nX2/1nX2 = 36.7/32 = 1.146875
    dy = 2nY2/1nY2 = 10.2/20 = 0.51

    Now, I believe you can convert between (X,Y) in System 1 to (X,Y) in System 2 by the following
    (Xin, Yin)
    shift to system1 0,0 coordinates, multiply by scale, shift to System 2 coordinates
    Xout = (Xin - 1X1) * dx + 2X1
    Yout = (Yin - 1Y1) * dy + 2Y1

    Likewise, from system2 to system1 would be the inverse

    shift to system2 0,0 coordinates, divide by scale, shift to System 1 coordinates
    Xout = (Xin - 2X1) / dx + 1X1
    Yout = (Yin - 2Y1) / dy + 1Y1

    I don't know.
    I could be wrong. Don't really feel like trying to verify it.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2012
    Posts
    118

    Re: Converting between coordinate systems

    Thanks passel...

    I did a verification, but sadly it did not work.

    I do very much appreciate the attempt!

    R

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Converting between coordinate systems

    I thought of a simple way to verify it, and don't bother the code doesn't work at all.
    I got to thinking about it and I figured it wouldn't work, because this code is just treating the two X and Y axis as always being aligned but with different scaling.
    Of course, we know this isn't the case. The scales should essentially be the same (you said both in millimeters originally), but rotated and offset from one another.
    So, the code above doesn't take into consideration rotation at all.

    Ok, so I don't know the math right now necessary to choose the X,Y offset values and rotation for the matrix, but to test the previous post, it is really easy to visually create the matrix rotation value and offsets by essentially doing what you do with the charts.
    Draw a line using the first coordinates, then draw another line using the second coordinates.
    Then allow dragging on the drawing to change the rotation value and X,Y offsets applied to the drawing matrix until you have one line laid over the other.
    At that point, you now know what the angle and offsets need to be (found empirically), and can use an inverse matrix to translate from the mouse input, (i.e. your 1st systems coordinates) to the second Systems coordinates.

    So, using your original values in this test code (9,7)-(41,27) and (6.5,11.2)-(43.2,21.4), you can try the code below that proved that the previous post was garbage.
    This code doesn't do anything to adjust the fact that your coordinates are either a little off (one line is a bit longer than the other), or that both scales are not really the same, one is 1.009 times bigger than the other.

    I recommend dragging with the right mouse button first, to try to get the lines parallel, then dragging with the left mouse button to overlay the lines, lining up the upper left end of the lines.
    The title bar will show the resulting matrix translation (Xoff, Yoff) and Angle value, and show the Sys1 coordinates (PtIn and Sys2 coordinates (PtOut: ) as you move the mouse around (PtOut: of course won't be "correct" until you've lined the lines up).
    Code:
    Public Class Form1
    
      Private Ang As Single
      Private Xoff, Yoff As Single
      Private mInv As New Drawing2D.Matrix
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        DoubleBuffered = True
      End Sub
    
      Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Static lPos As Point
        If e.Button = Windows.Forms.MouseButtons.Left Then
          Xoff += (e.X - lPos.X) / 10.0F
          Yoff += (e.Y - lPos.Y) / 10.0F
          Refresh()
        ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
          Ang += (e.X - lPos.X) / 10.0F
          Refresh()
        End If
        lPos = e.Location
        Dim sPnt As PointF = New PointF(lPos.X / 10.0F, lPos.Y / 10.0F)
        Dim nPnt() As PointF = {New PointF(lPos.X, lPos.Y)}
        mInv.TransformPoints(nPnt)
        Me.Text = String.Format("Xoff: {0}, Yoff: {1}, Ang: {2}, PtIn: {3} PtOut: {4}",
                                Xoff.ToString("F1"), Yoff.ToString("F1"), Ang.ToString("F2"), sPnt.ToString, nPnt(0).ToString)
      End Sub
    
      Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim g As Graphics = e.Graphics
        Using bPen As New Pen(Brushes.Black, 0)
          Using rPen As New Pen(Brushes.Red, 0)
    
            g.ScaleTransform(10, 10)
            g.DrawLine(bPen, 9, 7, 41, 25)
            g.TranslateTransform(Xoff, Yoff)
            g.RotateTransform(Ang)
            mInv = g.Transform.Clone
            mInv.Invert()
            g.DrawLine(rPen, 6.5, 11.2, 43.2, 21.4)
          End Using
        End Using
    
      End Sub
    End Class

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Converting between coordinate systems

    Forgot to mention I scaled the drawing up 10x (g.ScaleTransform(10, 10)), to make it easier to align the end point more precisely (the reason there are a lot of /10.0Fs in the code).
    Last edited by passel; Oct 7th, 2015 at 07:01 PM.

  12. #12
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Converting between coordinate systems

    I think I understand what you want from post #4, which is a function f(x, y) = (u, v) which sends two fixed pairs (x1, y1), (x2, y2) to two fixed pairs (u1, v1), (u2, v2) while scaling distance at a constant rate and (I think) reversing orientation. One way to compute such a function is to decompose any point (x, y) into the point on the line connecting (x1, y1) and (x2, y2) closest to (x, y) together with a signed distance from (x, y) to that line. Linearly interpolate the point along the line, and go the negative of the signed distance scaled by the overall scaling factor away from the line connecting (u1, v1), (u2, v2) to find f(x, y).

    Another approach would be to construct a sequence of linear transformations with the desired properties, which is probably closer to what you're doing by hand. More precisely, translate (x, y) space so that (x1, y1) = (u1, v1). Then scale (x, y) space so that the distance between the two pairs agree, and rotate (x, y) space around (x1=u1, y1=v1) until (x2, y2) = (u2, v2). Finally reverse orientation, which is clear physically but is a bit tedious rigorously; one approach is to apply the above with (u1, v1)=(0, 0), (u2, v2)=(1, 0), flip the y-axis, and apply it again to send (0, 0) and (1, 0) back to (u1, v1), (u2, v2).

    The second approach has the benefit of being more natural physically and expressing the final answer as an affine transformation in the usual form--an overall translation together with a matrix multiplication. It's probably longer in code and a bit more tedious. So, I'll expand on the first approach, which is a bit more magical.

    1. Given (x, y), let (x0, y0) be the closest point to (x, y) on the line connecting (x1, y1) and (x2, y2). Here's a thread saying how.
    2. Let d be the signed distance from (x, y) to (x0, y0). Computing |d| is easy--just use the distance formula. Computing the sign isn't so bad either. The idea is to use the sign of the dot product of a vector perpendicular to the vector connecting (x1, y1) and (x2, y2) and the vector connecting (x0, y0) and (x, y). Hence use the sign of the dot product of (y2 - y1, x1 - x2) and (x - x0, y - y0). (If the dot product is zero, that's fine since d will be zero anyway.)
    3. Let s be the overall scale factor, namely the distance from (u1, v1) to (u2, v2) divided by the distance from (x1, y1) to (x2, y2).
    4. Compute f(x0, y0) = (u0, v0) by linear interpolation. More precisely, find t such that (x0, y0) = t(x1, y1) + (1-t)(x2, y2) and let (u0, v0) = t(u1, v1) + (1-t)(u2, v2).
    5. Compute f(x, y) = (u, v) = (u0, v0) - s*d(v2 - v1, u1 - u2)/|(v2 - v1, u1 - u2)|. This is telling you to travel from (u0, v0) a distance of s*d perpendicular to the line connecting (u1, v1) and (u2, v2), where I've fiddled with the sign choices to get an orientation reversal.

    I can be more specific if you need. Conceptually what I said is perfectly clear and I'm certain it's correct, though it's easy to make typos or have little bugs in steps before something like this is implemented. Oh, I also didn't attempt to give the cleanest formulas--one could probably take better advantage of dot products and avoid some distance computations.
    Last edited by jemidiah; Oct 9th, 2015 at 03:47 AM.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  13. #13
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Converting between coordinate systems

    Marked as resolved in this parallel thread.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width