How to draw a line based on the center point and angle in iOS?

This is an iOS issue, as it is my current inability to do coordinate geometry. Given that CGPoint acts as the point through which the line passes and the angle in radians. How to draw a line passing through the borders of the screen (endless line)?

I use Quartz2d for this, and the API for creating a string is limited to two input points. So, how do I convert a point and an angle to two points on the borders of an iOS device?

+6
source share
1 answer

It starts with simple trigonometry. You need to calculate the x and y coordinate of the second point. With a start of 0.0 and processing a line that goes right to the right at 0 degrees and goes counterclockwise (against some of you counterclockwise), you do:

double angle = ... // angle in radians double newX = cos(angle); double newY = sin(angle); 

This assumes a radius of 1. Multiply the desired radius each time. Choose a number that will be larger than the screen, for example, 480 for iPhone or 1024 for iPad (assuming you need dots, not pixels).

Then add the starting point to get the ending point.

Assuming you have a CGPoint start , double angle and length, your endpoint is:

 double endX = cos(angle) * length + start.x; double endY = sin(angle) * length + start.y; CGPoint end = CGPointMake(endX, endY); 

This is normal if the endpoint is off.

+8
source

All Articles