geometry - Where a vector would intersect the screen if extended towards it's direction (swift) -


i'm trying write function in swift, returns cgpoint extension of vector (which within screen) intersect screen. let's assume screen 800 x 600. it's scheme:

the screen scheme

the function should have following parameters:

func calcpoint(start: cgpoint, end: cgpoint) -> cgpoint 
  • start: cgpoint(x: x1, y: y1) - beginning of vector.
  • end: cgpoint(x: x1, y: y1) - end point of vector.
  • the return point 1 @ vector intersects screen (cgpoint(x: x3, y: y3) shown @ scheme).

the values vector start , end aways points within screen (the rectangle 0, 0, 800, 600).

edit (for alexander): there formula, in given situation make easy write function, in not obvious way using if ... else ... , triangle vertices ratio?

to compute point e can @ triangles given setting. have triangle abc , dbe. note similar, such can set following relation ab : ac = db : de using intercept theorem (ab etc. stands line segment between , b). in given setting know points e.

enter image description here

using start , end points given setting:

enter image description here

in case start , end have same x or y-coordinate top bottom or left right border same coordinate.

using absolute values should work 4 corners of rectangle. of course have consider e being out of rectangle, again same relation can used ab : ac = d'b : d'e'

enter image description here


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -