Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Flip a list of Vector2 points upside down

Discussion in 'Scripting' started by alexfirecheetah, Dec 29, 2022.

  1. alexfirecheetah

    alexfirecheetah

    Joined:
    Jul 8, 2020
    Posts:
    10
    So I have several lists of vector2 points that are stored with the top left corner being (0,0) but I need the bottom left corner to be (0,0). Each one of these lists represent a shape, and I need the whole thing to be flipped on the Y axis as one, not each individual shape in place.

    I have tried 2 methods.

    Number #1: I have tried to use the biggest Y value from any of the shapes, and subtract all Y values in all shapes from it.
    Code (CSharp):
    1. Dictionary<string, List<Vector2>> flippedShapePoints = new Dictionary<string, List<Vector2>>();
    2.         float maxY = shapePoints.Values.Max(shape => shape.Max(point => point.y));
    3.         foreach (string shape in shapePoints.Keys)
    4.         {
    5.             List<Vector2> newPoints = new List<Vector2>();
    6.             foreach (Vector2 point in shapePoints[shape])
    7.             {
    8.                 Vector2 newPoint = new Vector2(point.x, maxY - point.y);
    9.                 newPoints.Add(newPoint);
    10.             }
    11.             flippedShapePoints.Add(shape, newPoints);
    12.         }
    Number #2: I have tried to use the canvas Y value and subtract all other Y values from it.
    Code (CSharp):
    1. Dictionary<string, List<Vector2>> flippedShapePoints = new Dictionary<string, List<Vector2>>();
    2.         foreach (string shape in shapePoints.Keys)
    3.         {
    4.             List<Vector2> newPoints = new List<Vector2>();
    5.             foreach (Vector2 point in shapePoints[shape])
    6.             {
    7.                 Vector2 newPoint = new Vector2(point.x, scene.SceneViewport.height - point.y);
    8.                 newPoints.Add(newPoint);
    9.             }
    10.             flippedShapePoints.Add(shape, newPoints);
    11.         }
    But neither of these methods seem to work. Any help on why this isn't working would be appreciated or how I would do this. Thanks.

    (If you try to use the biggest Y value in each shape and subtract all Y values in just that shape by it, then you flip that shape upside down. But when I try to do it to all of the shapes together with these methods above, it doesn't end up working).
     
  2. frenchynyc

    frenchynyc

    Joined:
    May 14, 2015
    Posts:
    28
    Hello,

    It looks like your first approach is close, but you are finding the maximum Y value across all shapes and subtracting all Y values from it. Instead, you should find the maximum Y value for each shape separately and then subtract all Y values in that shape from it.

    Here is an updated version of your code using this approach:
    Code (CSharp):
    1. Dictionary<string, List<Vector2>> flippedShapePoints = new Dictionary<string, List<Vector2>>();
    2. foreach (string shape in shapePoints.Keys)
    3. {
    4.     List<Vector2> newPoints = new List<Vector2>();
    5.     float maxY = shapePoints[shape].Max(point => point.y);
    6.     foreach (Vector2 point in shapePoints[shape])
    7.     {
    8.         Vector2 newPoint = new Vector2(point.x, maxY - point.y);
    9.         newPoints.Add(newPoint);
    10.     }
    11.     flippedShapePoints.Add(shape, newPoints);
    12. }
    13.  
    This should correctly flip each shape individually on the Y axis so that the bottom left corner is (0,0).
     
    halley likes this.
  3. alexfirecheetah

    alexfirecheetah

    Joined:
    Jul 8, 2020
    Posts:
    10
    I tried that first too, but this flipped each shape individually like you said. The image as a whole made from multiple of these shapes didn't get flipped, its just that these shapes got flipped individually and remained in place. Here is an image of the upside down rendered image and with your code:

    Original:
    original.png
    With your code:
    after.png
    What im hoping for:
    expected.png
    As you can see, it did flip the triangle, just it didn't flip the shape as a whole. What I am looking for is code to flip and reposition all of the shapes so that the image doesn't appear upside down anymore.

    Thanks for the idea though, I appreciate it, its just not quite what i'm looking for.
     
  4. frenchynyc

    frenchynyc

    Joined:
    May 14, 2015
    Posts:
    28
    To flip the shapes as a whole, you'll need to apply a transformation to the points that make up each shape. One way to do this would be to apply a reflection transformation, which flips the points across a line of symmetry.

    To reflect the points across the y-axis (the vertical axis), you can apply the following transformation to each point:
    Code (CSharp):
    1. Vector2 reflectedPoint = new Vector2(point.x, -point.y);
    2.  
    This will produce a reflection of the point across the y-axis. You can then add these reflected points to the newPoints list, and add this list to the flippedShapePoints dictionary.

    Alternatively, you can use a matrix transformation to perform the reflection. A matrix transformation allows you to apply a combination of translations, rotations, and scalings to a set of points. To reflect the points across the y-axis using a matrix transformation, you can use the following matrix:
    Code (CSharp):
    1. Matrix4x4 reflectionMatrix = new Matrix4x4(
    2.     1, 0, 0, 0,
    3.     0, -1, 0, 0,
    4.     0, 0, 1, 0,
    5.     0, 0, 0, 1
    6. );
    7.  
    Then, you can apply the transformation to each point using the following code:
    Code (CSharp):
    1. Vector2 reflectedPoint = reflectionMatrix.MultiplyPoint(point);
    2.  
    This will produce a reflection of the point across the y-axis. You can then add these reflected points to the newPoints list, and add this list to the flippedShapePoints dictionary as before.

    I hope this helps! Let me know if you have any questions or if you'd like more information.
     
    alexfirecheetah likes this.
  5. alexfirecheetah

    alexfirecheetah

    Joined:
    Jul 8, 2020
    Posts:
    10
    This is very helpful and I will definitely try this. Thanks for all of the help and I will let you know if it doesn't work but otherwise I think I will be able to build off of this. Tyvm, appreciate it!
     
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,925
    I've used the "subtract from highest y" method and it should work. In fact, both method should work to at least flip them. Either way, you're subtracting every y from a constant value. At worst they should be shifted up or down (but still flipped).

    I'm wondering if you're using these as points in a model with backface culling? If so, flipping the y's flips the windings. Try looking at them from the back and you may see them. I think any easy fix might be to reverse each list.