Page 1 of 1

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

Posted: Wed Oct 06, 2021 6:00 am
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...

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

Posted: Wed Oct 06, 2021 7:12 am
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?

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

Posted: Wed Oct 06, 2021 7:21 am
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.

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

Posted: Wed Oct 06, 2021 7:28 am
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

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

Posted: Wed Oct 06, 2021 7:32 am
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

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

Posted: Wed Oct 06, 2021 9:20 am
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

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

Posted: Wed Oct 06, 2021 10:03 am
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

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

Posted: Wed Oct 06, 2021 5:13 pm
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

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

Posted: Wed Oct 06, 2021 7:02 pm
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.

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

Posted: Thu Oct 07, 2021 3:08 pm
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!