I've created a 2d gradient shader which uses an absolute start/end point to determine how an arbitrary amount of colors are positioned along the gradient line. I now am creating an api to create a gradient with an angle to describe the direction the linear gradient gets drawn, and calculating the start/end points of the gradient based on that angle & the bounds of a quad. Running into an issue here finding those same start/end points which I'm struggling to solve..
This is a visual of the problem I'm trying to solve. I need to determine x,y
& x2, y2
based on any two (opposite) vectors. The vectors will always originate from the center of the rectangle, and can be in any direction (opposite of each other). Could also think of it as one line segment from x,y
-> x2,y2
. I've tried a trigonometry based approach here which almost kind of works but fails as the angle approaches the corner of the bounds. Hoping for a more clean solution using vector math which will be more efficient.
My initial thoughts are just to represent the rectangle as 4 axis-aligned line segments and attempt to calculate intersection that way, but struggling to figure out how to go about this.. I only have an angle right now that I'm using with the center point to calculate a unit vector in the proper direction. Any help appreciated!