Feature Suggestion: XY Point sets?

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
User avatar
MHD
Prole
Posts: 18
Joined: Fri Nov 14, 2008 11:45 pm
Location: Denmark

Feature Suggestion: XY Point sets?

Post by MHD »

I find it troubeling that löve handles all positions separately.

I have a tempoary solution scripted in lua, but that doesn't just cut it.

My curren't solution:

Code: Select all

point = {}

function IsPoint(self,assrt)
	if assrt then
		assert(type(self) == "table")
		assert(type(self.x) == "number")
		assert(type(self.y) == "number")
		assert(getmetatable(self))
		for k,v in pairs(getmetatable(self)) do
			assert(v == point.mt[k])
		end
	else
		if type(self) ~= "table" or type(self.x) ~= "number" or type(self.y) ~= "number" then return false end
		for k,v in pairs(getmetatable(self)) do
			if v ~= self[k] then return false end
		end
		return true
	end
end

function Point(x,y)
	local obj = {x = x or 0, y = y or 0}
	setmetatable(obj, point.mt)
	IsPoint(obj,true)
	return obj
end

point.mt = 	{
			__index = point,
			__add = function(a,b)
				IsPoint(a,true) IsPoint(b,true)
				return Point(a.x+b.x,a.y+b.y)
			end,
			__sub = function(a,b)
				IsPoint(a,true) IsPoint(b,true)
				return Point(a.x-b.x,a.y-b.y)
			end,
			__unm = function(a)
				return Point(-a.x,-b.y)
			end,
			__mul = function(a,b)
				if type(a) == "number" then
					return Point(a*b.x,a*b.y)
				end
				if type(b) == "number" then
					return Point(a.x*b,a.y*b)
				end
				IsPoint(a,true) IsPoint(a,true)
				return Point(a.x*b.x,a.y*b.y)
			end,
			__div = function(a,b)
				if type(a) == "number" then
					return Point(a/b.x,a/b.y)
				end
				if type(b) == "number" then
					return Point(a.x/b,a.y/b)
				end
				IsPoint(a,true) IsPoint(b,true)
				return Point(a.x/b.x,a.y/b.y)
			end,
			__concat = function(a,b)
				if IsPoint(a) then
					if IsPoint(b) then
						return "("..a.x..","..a.y..")".."("..b.x..","..b.y..")"
					end
						return "("..a.x..","..a.y..")"..b
				end
				if IsPoint(b) then
					return a.."("..b.x..","..b.y..")"
				end
			end,
			}

function point:dist(p)
	p = p or Point()
	IsPoint(p,true)
	local res = self-p
	return math.sqrt(res.x^2+res.y^2)
end

function point:normal(p)
	p = p or Point()
	IsPoint(p,true)
	return self/self:dist()+p
end

function point:u()
	return self.x,self.y
end
So please hear my plea, and make something like this ^
I r awsum at looah ^^
Dvondrake
Prole
Posts: 29
Joined: Sun Oct 26, 2008 5:53 pm

Re: Feature Suggestion: XY Point sets?

Post by Dvondrake »

---
Last edited by Dvondrake on Mon Jan 21, 2019 10:21 pm, edited 1 time in total.
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Feature Suggestion: XY Point sets?

Post by rude »

I don't understand what you want. Do you want a 2D Vector object? Would you like this:

Code: Select all

love.graphics.draw( object, position )
Instead of this?

Code: Select all

love.graphics.draw( object, x, y )
Don't see what's wrong with doing that in Lua (i.e. like you do it). ;)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 8 guests