How to calculate a coordinate on an arc?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
petervh
Prole
Posts: 2
Joined: Fri Nov 23, 2018 4:08 am

How to calculate a coordinate on an arc?

Post by petervh »

Hi, sorry for a basic question---I'm very new. :)

I am trying to find the coordinates of a specific point on an arc. I'm using getWidth & get Height to scale my Arc to different window/screen sizes.

This is my basic code for drawing an arc:

Code: Select all

function love.load()
    	 width = love.graphics.getWidth( )
	 height = love.graphics.getHeight( )
	 love.window.setMode(width, height, {resizable=true, vsync=false, minwidth=400, minheight=300})
end

function love.draw( )
	 width = love.graphics.getWidth( )
	 height = love.graphics.getHeight( )
	 local x = 1
	 local y = 6

	 local radius = (y*.15)*(height/2)
 	 local angle1 = x*(math.pi/-8)
 	 local angle2 = math.pi
	 love.graphics.setLineWidth(2)

	 love.graphics.arc("line",  width/2, height/2, radius, angle1, angle2)
end
In the attached image, I have colored a red circle over the point in the arc which I need to determine. Keep in mind that I am actually drawing many dozens of arcs, so I certainly need to determine this through an algorithmic process.

Thanks in advance for any advice that comes. :D
Attachments
arc.png
arc.png (47.03 KiB) Viewed 2214 times
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How to calculate a coordinate on an arc?

Post by ivan »

From what I understand you want to convert polar coordinates to Cartesian?

Code: Select all

local offsetX = love.graphics.getWidth()/2
local offsetY = love.graphics.getHeight()/2

local angle = math.rad(45)
local radius = 100

local x = math.cos(angle)*radius + offsetX
local y = math.sin(angle)*radius + offsetY
Note that your angle1 and angle2 variables are somewhat confusing, I think it would be more clear if you used degrees:

Code: Select all

local angle1 = math.rad(25)
local angle2 = math.rad(180)
Further reading:
https://2dengine.com/?p=vectors#Angles_and_rotation
petervh
Prole
Posts: 2
Joined: Fri Nov 23, 2018 4:08 am

Re: How to calculate a coordinate on an arc?

Post by petervh »

Wonderful, thank you Ivan for dissecting my issue.
Your code works, functions as I was hoping. Your reading reference is especially helpful.
This is an excellent forum.

In case someone is searching to solve the same problem, here is the working code:

Code: Select all

function love.load()
	 love.window.setMode(800, 600, {resizable=true, vsync=false, minwidth=400, minheight=300})
end

function love.draw( )
 	 local angle1 = math.rad(180)
	 local angle2 = math.rad(130)
	 
	 local offsetX = love.graphics.getWidth()/2
	 local offsetY = love.graphics.getHeight()/2
	 local radius = 300

	 local x1 = math.cos(angle1)*radius + offsetX
	 local y1 = math.sin(angle1)*radius + offsetY

	 local x2 = math.cos(angle2)*radius + offsetX
	 local y2 = math.sin(angle2)*radius + offsetY

	 love.graphics.setLineWidth(2)
	 love.graphics.arc("line",  offsetX, offsetY, radius, angle1, angle2)
	 
	 love.graphics.print(x1,x1,y1)
	 love.graphics.print(y1,x1, y1+40)
	 
	 love.graphics.print(x2,x2,y2) 
	 love.graphics.print(y2,x2, y2+40)
end
Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests