Search Unity

How can I find the reflection of a point in a perpendicular line

Discussion in 'Editor & General Support' started by nowletsstart, Jun 12, 2018.

  1. nowletsstart

    nowletsstart

    Joined:
    Jan 1, 2016
    Posts:
    78
    From the image below I know the positions A and B. How can I find positions C and D and the reflection (Er) of an object E in the line CD? Please note that I'm trying to achieve this in 2D.

    Screen Shot 2018-06-12 at 12.53.20 PM.png
     
    DMorock likes this.
  2. nowletsstart

    nowletsstart

    Joined:
    Jan 1, 2016
    Posts:
    78
    can someone please help me out
     
  3. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    Your description isn't really exact. Is CD exactly through the middle of AB?

    Assuming so, here's roughly how I'd attempt it:
    find the origin, where they cross - assuming the middle that'd be O=A+(B-A)/2
    find the perpendicular vector - one would be P={(b.y-a.y),(a.x-b.x)}
    find the direction from E to O -> EO=O-E
    use Vector2.Reflect to reflect EO to OEr -> OEr = Vector2.Reflect(EO, P)
    add OEr to O to get Er=OEr + O

    There may be some simpler way or some errors in this (normalizing missing probably), but it's one approach.
     
    DMorock likes this.