Function to find these 4 points? (simple math problem)

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
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

Function to find these 4 points? (simple math problem)

Post by Eglaios »

I have a point A (x,y), and want to generate 4 points around, like this :
Image
(A here is [0,0], but could have different coords)

I want to generate them from a for() loop into a table, so it would look like this :

Code: Select all

function genPoints(x,y) --Coords of A
 local points = {}
 local point = {}
 for i = 1, 4 do
  point.x, point.y = [?]
  point.otherData = [...]
  table.insert(points, point)
 end
 return points
end
Pretty sure it would be achievable using modulo and that kind of stuff, I'll still search, but if someone have an answer meanwhile, please share it, thanks!


Edit : I ended up with this :
point.x = x + (50*i-150)%100,
point.y = y + (50*i-100)%100,
...but the modulo automatically converts invert negative values... still searching...
Currently working on game music...
User avatar
BrotSagtMist
Party member
Posts: 604
Joined: Fri Aug 06, 2021 10:30 pm

Re: Function to find these 4 points? (simple math problem)

Post by BrotSagtMist »

I dont see why you want a for loop for just 4 points here, that is just overcomplicating it.
Are you planning to add more points later?
obey
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Function to find these 4 points? (simple math problem)

Post by darkfrei »

Code: Select all

for i = 1, 4 do
  point.x, point.y = -50*math.cos(i*math.pi/2), 50*math.sin(i*math.pi/2)
  point.otherData = [...]
  table.insert(points, point)
 end
:awesome: But positive y is bottom, not at the top.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
BrotSagtMist
Party member
Posts: 604
Joined: Fri Aug 06, 2021 10:30 pm

Re: Function to find these 4 points? (simple math problem)

Post by BrotSagtMist »

random pointnumbers:

Code: Select all

num=4 
len=50
for x=0, math.pi*2-0.01,math.pi*(2/num) do
 print(math.cos(x)*len,math.sin(x)*len) -- +point of origin
end
Last edited by BrotSagtMist on Wed Oct 06, 2021 7:36 am, edited 1 time in total.
obey
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Function to find these 4 points? (simple math problem)

Post by darkfrei »

also

Code: Select all

for i, p in ipairs ({{x=1,y=0}, {x=0,y=1}, {x=-1,y=0}, {x=0,y=-1}}) do
	table.insert(points, {x=p.x*50, y=p.y*50})
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

Re: Function to find these 4 points? (simple math problem)

Post by Eglaios »

BrotSagtMist wrote: Wed Oct 06, 2021 7:12 am Are you planning to add more points later?
These are particles, and the full loop looks like this :

Code: Select all

function particleEvent(x,y)
 for i=1, 4 do
  table.insert(particles,{
   posX = x + (50*i-150)%100,
   posY = y + (50*i-100)%100,
   particleData = ...,
   particleData2 = ...,
   etc.
  })
 end
end
I could simply have different lines for each of the 4 particles but it'd really look cleaner if I could manage to fit that into the loop itself...

I'll look at the answers and see if something works
Currently working on game music...
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Function to find these 4 points? (simple math problem)

Post by darkfrei »

Eglaios wrote: Wed Oct 06, 2021 6:00 am I want to generate them from a for() loop into a table, so it would look like this :

Code: Select all

function genPoints(x,y) --Coords of A
 local points = {}
 local point = {} -- not in the loop
 for i = 1, 4 do
  point.x, point.y = [?]
  point.otherData = [...]
  table.insert(points, point)
 end
 return points
end
Please note that you insert here THE SAME POINT!

The point definition must be in the loop:

Code: Select all

function genPoints(x,y) --Coords of A
 local points = {}
 for i = 1, 4 do
  local point = {} -- in the loop
  point.x, point.y = [?]
  point.otherData = [...]
  table.insert(points, point)
 end
 return points
end
Last edited by darkfrei on Wed Oct 06, 2021 10:51 am, edited 2 times in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

Re: Function to find these 4 points? (simple math problem)

Post by Eglaios »

darkfrei wrote: Wed Oct 06, 2021 7:21 am

Code: Select all

for i = 1, 4 do
  point.x, point.y = -50*math.cos(i*math.pi/2), 50*math.sin(i*math.pi/2)
  point.otherData = [...]
  table.insert(points, point)
 end
Looks perfect for me! All the operations are written next to x and y without touching the loop

darkfrei wrote: Wed Oct 06, 2021 7:21 am :awesome: But positive y is bottom, not at the top.
I actually forgot to mention that my coordinates system is upside down :
It's basically a vertical-scrolling "climb-the-tower" game, so Y=0 is the floor, and it increases as it goes up
Currently working on game music...
User avatar
BrotSagtMist
Party member
Posts: 604
Joined: Fri Aug 06, 2021 10:30 pm

Re: Function to find these 4 points? (simple math problem)

Post by BrotSagtMist »

I still dont get what you try to archieve here.
Writing a loop instead
points={x,y+50,x+50,y,x,y-50,x-50,y} ?
*scratches head*
There is nothing clean about this loop.
obey
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Function to find these 4 points? (simple math problem)

Post by milon »

I'd recommend either darkfrei or BrotSagtMist's approach to this. There's absolutely no benefit to using trig (sin/cos functions) unless you must - it's computationally expensive, less easy to maintain, etc.

Also, if you need to do a bunch of stuff with each particle, create a separate function to handle that. Then you just call that function 4 times - one for each point. Much cleaner!
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 14 guests