This is not a question specific to one programming language, more of a mathematically conceptual, though just in case, I'm using C++ on Visual Studio.
Basically, my current code draws a line, that starts at the centre (of the window), and ends at my mouse position at any time, every frame - I end up with a line that follows my mouse, starting from the centre.
My question is, how would I end up with the exact same system, except that no matter how far my mouse goes from the centre, the line will still follow the direction of the vector 'centerToMouse', but its length will only ever be 100 units (once the distance between MousePos and centre exceeds 100), such that I end up with a line that follows (extends/shrinks) my mouse, but once I reach above 100 units away from the centre, the line stays 100 units long as long as my mouse is further than 100 away.
I'm sorry if the question is badly phrased, in my head it makes sense, and I don't know how else to word it.
I don't necessarily need a code answer for C++, just the concept. I've tried a few methods involving normalizing, unit vectors etc. But I'm just stuck.
Thanks a lot for taking the time!
Paraphrasing from my above comment:
radius = 100;
angle = atan2(mouse_position.y-center.y, mouse_position.x-center.x);
if (distance(center, mouse_position) < radius){
line_position = mouse_position;
}
else{
line_position = center + Vector(radius*cos(angle), radius*sin(angle));
}