Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Converting Y up to Y down coordinates

Converting Y up to Y down coordinates

Scheduled Pinned Locked Moved C#
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SledgeHammer01
    wrote on last edited by
    #1

    My brain and my Google skills are sucking this late at night. Lets say I have a control height of 30 pixels Lets say my data range is -5 to 15 I want to normalize my coordinates, but flipped in Y up fashion. I.e. -5 is Y=29 on the screen, 15 is Y=0 Been googling transformation, coordinate systems, translation, etc. Lol, can't find a darn thing... I know this is simple math LOL. Only thing I'm coming up with this late at night is +5 all the points to get to a 0 origin, * 0.66 to scale and then 29 - that... but that can't be right...

    L B 2 Replies Last reply
    0
    • S SledgeHammer01

      My brain and my Google skills are sucking this late at night. Lets say I have a control height of 30 pixels Lets say my data range is -5 to 15 I want to normalize my coordinates, but flipped in Y up fashion. I.e. -5 is Y=29 on the screen, 15 is Y=0 Been googling transformation, coordinate systems, translation, etc. Lol, can't find a darn thing... I know this is simple math LOL. Only thing I'm coming up with this late at night is +5 all the points to get to a 0 origin, * 0.66 to scale and then 29 - that... but that can't be right...

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I am sure there is a proper mathematical way to do it, but this is what I figured out:

      -5 to 15 = 0 to 20 is 21 'points'
      0 to 29 is 30 pixels

      thus

      Y = ((y + 5) / 21 * 30) // transform 'points' to pixels
      Y' = 29 - Y // flip down to up

      OriginalGriffO S 4 Replies Last reply
      0
      • L Lost User

        I am sure there is a proper mathematical way to do it, but this is what I figured out:

        -5 to 15 = 0 to 20 is 21 'points'
        0 to 29 is 30 pixels

        thus

        Y = ((y + 5) / 21 * 30) // transform 'points' to pixels
        Y' = 29 - Y // flip down to up

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Shouldn't that be done the other way round?

        Y = ((y + 5) * 30) / 21 // transform 'points' to pixels

        Since all the values are given as integers (and are likely to be integers at source) doing the division first will not give you a good result.

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        L S 2 Replies Last reply
        0
        • OriginalGriffO OriginalGriff

          Shouldn't that be done the other way round?

          Y = ((y + 5) * 30) / 21 // transform 'points' to pixels

          Since all the values are given as integers (and are likely to be integers at source) doing the division first will not give you a good result.

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Yes, but I tested it on my calculator and it worked fine. :laugh: Actually, it would be better to use float values since Windows forms accepts them as co-ordinates.

          OriginalGriffO 1 Reply Last reply
          0
          • L Lost User

            Yes, but I tested it on my calculator and it worked fine. :laugh: Actually, it would be better to use float values since Windows forms accepts them as co-ordinates.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            So it's even unit tested? Ship it then! :laugh: It would be better to use floats, but you know and I know how few people round here think of that... :sigh:

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            S 2 Replies Last reply
            0
            • S SledgeHammer01

              My brain and my Google skills are sucking this late at night. Lets say I have a control height of 30 pixels Lets say my data range is -5 to 15 I want to normalize my coordinates, but flipped in Y up fashion. I.e. -5 is Y=29 on the screen, 15 is Y=0 Been googling transformation, coordinate systems, translation, etc. Lol, can't find a darn thing... I know this is simple math LOL. Only thing I'm coming up with this late at night is +5 all the points to get to a 0 origin, * 0.66 to scale and then 29 - that... but that can't be right...

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #6

              Thank you for my Sunday puzzle !

              // version two, passes test of reversing the two sample ranges defined here
              private Dictionary MapRangeValues(bool flipSign, List sourceRange, List mapToRange)
              {
              var dctTransform = new Dictionary();

              double ratio = Convert.ToDouble(mapToRange.Max() - mapToRange.Min()) / (sourceRange.Max() - sourceRange.Min());
              
              int offset = mapToRange\[0\] - sourceRange\[0\] + 1;
              
              foreach (var sInt in sourceRange)
              {
                  int val = Convert.ToInt32(ratio\*sInt) + offset;
                      //(sInt + offset));
              
                  if (flipSign)
                  {
                      val = mapToRange.Max() - val - 1;
                  }
                  else
                  {
                      val--;
                  }
              
                  dctTransform.Add(sInt, val);
              }
              
              return dctTransform;
              

              }

              // some test data
              private List DataRange = Enumerable.Range(-5, 21).ToList();

              private List ControlRange = Enumerable.Range(0, 30).ToList();

              // requires two TextBoxes on a Form ...
              private void testTheSucker()
              {
              textBox1.Clear();
              textBox2.Clear();

              var result1 = MapRangeValues(true, DataRange, ControlRange);
              
              var result2 = MapRangeValues(false, ControlRange, DataRange);
              
              foreach (var kvp in result1)
              {
                  textBox1.Text += string.Format("{0}\\t{1}\\r\\n", kvp.Key, kvp.Value);
              }
              
              foreach (var kvp in result2)
              {
                  textBox2.Text += string.Format("{0}\\t{1}\\r\\n", kvp.Key, kvp.Value);
              }
              

              }

              I am curious to see if this is as general purpose as I intend it to be: so, will test further. I am sure this can be improved (perhaps replaced with a few lines of Linq wizardry ?), and I'd welcome any feedback, corrections, ideas ! // first test results -5 29 -4 28 -3 26 -2 25 -1 23 0 22 1 21 2 19 3 18 4 16 5 15 6 13 7 12 8 10 9 9 10 8 11 6 12 5 13 3 14 2 15 0 // second test results 0 -5 1 -4 2 -4 3 -3 4 -2 5 -2 6 -1 7 0 8 1 9 1 10 2 11 3 12 3 13 4 14 5 15 5 16 6 17 7 18 7 19 8 20 9 21 9 22 10 23 11 24 12 25 12 26 13 27 14 28 14 29 15

              «If you search in Google for 'no-one ever got fired for buying IBM:' the top-hit is the Wikipedia article on 'Fear, uncertainty and doubt'»  What does that tell you about sanity in these times?

              S 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Shouldn't that be done the other way round?

                Y = ((y + 5) * 30) / 21 // transform 'points' to pixels

                Since all the values are given as integers (and are likely to be integers at source) doing the division first will not give you a good result.

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                S Offline
                S Offline
                SledgeHammer01
                wrote on last edited by
                #7

                Actually, this is in a WPF application, so by WPF convention, its an IEnumerable of doubles :). LOL, I guess my late at night answer was pretty close... I confused the whole 0 - 29 / 30 / 20 / 21 thing haha...

                1 Reply Last reply
                0
                • L Lost User

                  I am sure there is a proper mathematical way to do it, but this is what I figured out:

                  -5 to 15 = 0 to 20 is 21 'points'
                  0 to 29 is 30 pixels

                  thus

                  Y = ((y + 5) / 21 * 30) // transform 'points' to pixels
                  Y' = 29 - Y // flip down to up

                  S Offline
                  S Offline
                  SledgeHammer01
                  wrote on last edited by
                  #8

                  Ah, so I was pretty close... I guess I shouldn't try to write code at 11pm LOL... got thrown off by the 0 / 1 0 - 29 = 30 points thing haha... when I woke up this morning, I was like what the hell was I thinking?

                  L 1 Reply Last reply
                  0
                  • B BillWoodruff

                    Thank you for my Sunday puzzle !

                    // version two, passes test of reversing the two sample ranges defined here
                    private Dictionary MapRangeValues(bool flipSign, List sourceRange, List mapToRange)
                    {
                    var dctTransform = new Dictionary();

                    double ratio = Convert.ToDouble(mapToRange.Max() - mapToRange.Min()) / (sourceRange.Max() - sourceRange.Min());
                    
                    int offset = mapToRange\[0\] - sourceRange\[0\] + 1;
                    
                    foreach (var sInt in sourceRange)
                    {
                        int val = Convert.ToInt32(ratio\*sInt) + offset;
                            //(sInt + offset));
                    
                        if (flipSign)
                        {
                            val = mapToRange.Max() - val - 1;
                        }
                        else
                        {
                            val--;
                        }
                    
                        dctTransform.Add(sInt, val);
                    }
                    
                    return dctTransform;
                    

                    }

                    // some test data
                    private List DataRange = Enumerable.Range(-5, 21).ToList();

                    private List ControlRange = Enumerable.Range(0, 30).ToList();

                    // requires two TextBoxes on a Form ...
                    private void testTheSucker()
                    {
                    textBox1.Clear();
                    textBox2.Clear();

                    var result1 = MapRangeValues(true, DataRange, ControlRange);
                    
                    var result2 = MapRangeValues(false, ControlRange, DataRange);
                    
                    foreach (var kvp in result1)
                    {
                        textBox1.Text += string.Format("{0}\\t{1}\\r\\n", kvp.Key, kvp.Value);
                    }
                    
                    foreach (var kvp in result2)
                    {
                        textBox2.Text += string.Format("{0}\\t{1}\\r\\n", kvp.Key, kvp.Value);
                    }
                    

                    }

                    I am curious to see if this is as general purpose as I intend it to be: so, will test further. I am sure this can be improved (perhaps replaced with a few lines of Linq wizardry ?), and I'd welcome any feedback, corrections, ideas ! // first test results -5 29 -4 28 -3 26 -2 25 -1 23 0 22 1 21 2 19 3 18 4 16 5 15 6 13 7 12 8 10 9 9 10 8 11 6 12 5 13 3 14 2 15 0 // second test results 0 -5 1 -4 2 -4 3 -3 4 -2 5 -2 6 -1 7 0 8 1 9 1 10 2 11 3 12 3 13 4 14 5 15 5 16 6 17 7 18 7 19 8 20 9 21 9 22 10 23 11 24 12 25 12 26 13 27 14 28 14 29 15

                    «If you search in Google for 'no-one ever got fired for buying IBM:' the top-hit is the Wikipedia article on 'Fear, uncertainty and doubt'»  What does that tell you about sanity in these times?

                    S Offline
                    S Offline
                    SledgeHammer01
                    wrote on last edited by
                    #9

                    Wow! You went all out :). Richards answer was kind of a 1 liner though :). Although he wasn't converting a list which you are... Thanks!

                    1 Reply Last reply
                    0
                    • L Lost User

                      I am sure there is a proper mathematical way to do it, but this is what I figured out:

                      -5 to 15 = 0 to 20 is 21 'points'
                      0 to 29 is 30 pixels

                      thus

                      Y = ((y + 5) / 21 * 30) // transform 'points' to pixels
                      Y' = 29 - Y // flip down to up

                      S Offline
                      S Offline
                      SledgeHammer01
                      wrote on last edited by
                      #10

                      Wait a sec here... I'm testing this out... Lets say I pass in 0 through 10 So 0 should transform to 29 and 10 should transform to 0 0 through 10 is 11 pixels ((0 + 0) / 11 * 30) = 0, 29 - 0 = 29, so that works ((10 + 0) / 11 * 30) = 27.27, 29 - 27.27 = 1.73 which doesn't work. even if I do the multiplication first, ((10 + 0) * 30 / 11) = 27.27

                      1 Reply Last reply
                      0
                      • L Lost User

                        I am sure there is a proper mathematical way to do it, but this is what I figured out:

                        -5 to 15 = 0 to 20 is 21 'points'
                        0 to 29 is 30 pixels

                        thus

                        Y = ((y + 5) / 21 * 30) // transform 'points' to pixels
                        Y' = 29 - Y // flip down to up

                        S Offline
                        S Offline
                        SledgeHammer01
                        wrote on last edited by
                        #11

                        Yeah, I think something is amiss...

                        		List<double> lst = new List<double>();
                        
                        		lst.Add(0);
                        		lst.Add(3);
                        		lst.Add(7);
                        		lst.Add(10);
                        
                        		double dMin = lst.Min();
                        		double dMax = lst.Max();
                        
                        		double dHeight = 30;
                        
                        		//double y = 0; // 29 is correct
                        		double y = 10; // 1.72 not correct
                        
                        		double Y = ((y + Math.Abs(dMin)) \* dHeight) / (dMax - dMin + 1);
                        		Y = dHeight - 1 - Y;
                        
                        		System.Diagnostics.Debug.WriteLine(Y);
                        

                        With the -5 to 15 range, it gets closer, 0.43, so I guess that's a round off... if I truncate everything to ints, it comes out even worse, to 2 instead of 1.72.

                        1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          So it's even unit tested? Ship it then! :laugh: It would be better to use floats, but you know and I know how few people round here think of that... :sigh:

                          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                          S Offline
                          S Offline
                          SledgeHammer01
                          wrote on last edited by
                          #12

                          Hmm... seems more like this is the correct answer:

                          double Y = ((y + Math.Abs(dMin)) * (dHeight - 1)) / (dMax - dMin);
                          Y = dHeight - 1 - Y;

                          This works with:

                          //lst.Add(-5);
                          lst.Add(0);
                          lst.Add(3);
                          lst.Add(7);
                          lst.Add(10);
                          //lst.Add(15);

                          With the commented and uncommented values. dHeight = 30 (since the control is 30 pixels high, but the pixel range is 0 through 29) With the original formula, the 0 would get to 29 correctly, but the 10 would only go to 1.72. With the -5 / 15 range, it would go to 0.43. With this new formula, both ways got to 0 & 29.

                          1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            So it's even unit tested? Ship it then! :laugh: It would be better to use floats, but you know and I know how few people round here think of that... :sigh:

                            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                            S Offline
                            S Offline
                            SledgeHammer01
                            wrote on last edited by
                            #13

                            Ok, now I'm really confused... the min & max points are working now, but it seems like points in the middle are all wacky. If my control is 30 pixels high, the mid point is going to be 14, right? so if I give it 0, 3, 7, 10, my min & max is 0 & 10. So one would expect 5.5 to map to 14, but its mapping to 13.05 :confused:.

                            S 1 Reply Last reply
                            0
                            • S SledgeHammer01

                              Ok, now I'm really confused... the min & max points are working now, but it seems like points in the middle are all wacky. If my control is 30 pixels high, the mid point is going to be 14, right? so if I give it 0, 3, 7, 10, my min & max is 0 & 10. So one would expect 5.5 to map to 14, but its mapping to 13.05 :confused:.

                              S Offline
                              S Offline
                              SledgeHammer01
                              wrote on last edited by
                              #14

                              NM... I think I'm on crack again... LOL... the new formula does work. If my range is 0 to 10, the mid point is 5. And the mid point on a control 30 pixels high, the pixel range is 0 to 29, so the mid point is 14.5 :).

                              1 Reply Last reply
                              0
                              • S SledgeHammer01

                                Ah, so I was pretty close... I guess I shouldn't try to write code at 11pm LOL... got thrown off by the 0 / 1 0 - 29 = 30 points thing haha... when I woke up this morning, I was like what the hell was I thinking?

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #15

                                I've had many mornings like that.

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups