How draw a line

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Pedin
Prole
Posts: 16
Joined: Thu Aug 04, 2016 7:21 pm

How draw a line

Post by Pedin »

Hey guys, so I want to do a function that connects two points, I have a point already defined and a point b that is defined when I click on the screen, I need to draw circles from a to b, can somone help me ?
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: How draw a line

Post by Sir_Silver »

When you say draw circles from a to b, do you mean you should draw one circle, where the center of the circle is point a and the edge of the circle is point b?
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: How draw a line

Post by Ref »

"I need to draw circles from a to b"
Something like this?

Code: Select all

function drawCircles( p1, p2, dia )
	local sn = p2.x < p1.x and -1 or 1
	local an = math.tan( (p2.y-p1.y) / (p2.x-p1.x) )
	for x = p1.x, p2.x, 2*sn*dia do
		y = sn * x * math.atan( an ) + p1.y
		love.graphics.circle( 'line', x, y, dia )
		end
	end
Pedin
Prole
Posts: 16
Joined: Thu Aug 04, 2016 7:21 pm

Re: How draw a line

Post by Pedin »

Sir_Silver wrote:When you say draw circles from a to b, do you mean you should draw one circle, where the center of the circle is point a and the edge of the circle is point b?
i need draw little circles, like points in a cartesian plane.
Pedin
Prole
Posts: 16
Joined: Thu Aug 04, 2016 7:21 pm

Re: How draw a line

Post by Pedin »

Ref wrote:
"I need to draw circles from a to b"
Something like this?

Code: Select all

function drawCircles( p1, p2, dia )
	local sn = p2.x < p1.x and -1 or 1
	local an = math.tan( (p2.y-p1.y) / (p2.x-p1.x) )
	for x = p1.x, p2.x, 2*sn*dia do
		y = sn * x * math.atan( an ) + p1.y
		love.graphics.circle( 'line', x, y, dia )
		end
	end

i will test
User avatar
zorg
Party member
Posts: 3453
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How draw a line

Post by zorg »

>Draw a line
>Draw circles
>Draw points

[wiki]love.graphics.line[/wiki]
[wiki]love.graphics.circle[/wiki]
[wiki]love.graphics.points[/wiki]
You know, in case you're overcomplicating things
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: How draw a line

Post by Sir_Silver »

I'm pretty sure this isn't going to be exactly what you wanted, but maybe you can gather something from it. It is a "Cartesian Plane" where you can press lmb and start dragging, then release to create two points connected by a line.
graph.love
(577 Bytes) Downloaded 162 times
If you just want to see the code without downloading:

Code: Select all

local start_x, start_y
local draw_line = false

function love.load()
	love.window.setMode(600, 600)
	love.graphics.setBackgroundColor(255, 255, 255)
	background = love.graphics.newCanvas()
	
	love.graphics.setCanvas(background)
		for y = 0, 600 / 50 do
			for x = 0, 600 / 50 do
				love.graphics.setColor(0, 0, 0)
				love.graphics.setLineWidth(y == math.floor(600 / 50 / 2) and 2 or 1)
				love.graphics.line(0, y * 50, 600, y * 50)
				
				love.graphics.setLineWidth(x == math.floor(600 / 50 / 2) and 2 or 1)
				love.graphics.line(x * 50, 0, x * 50, 600)
			end
		end
	love.graphics.setCanvas()
end

function love.update(dt)
	if love.mouse.isDown(1) then
		if start_x and start_y then
			draw_line = true
		end
	else
		draw_line = false
	end
end

function love.draw()
	love.graphics.setColor(255, 255, 255)
	love.graphics.draw(background)
	
	if draw_line then
		local end_x, end_y = love.mouse.getPosition()
		
		love.graphics.setColor(0, 0, 0)
		love.graphics.circle("fill", end_x, end_y, 5)
		love.graphics.line(start_x, start_y, end_x, end_y)
	end
end

function love.mousepressed(x, y, button)
	if button == 1 then
		love.graphics.setCanvas(background)
			love.graphics.setColor(0, 0, 0)
			love.graphics.circle("fill", x, y, 5)
		love.graphics.setCanvas()
		
		start_x, start_y = x, y
	end
end

function love.mousereleased(x, y, button)
	if button == 1 then
		love.graphics.setCanvas(background)
			love.graphics.setColor(0, 0, 0)
			love.graphics.circle("fill", x, y, 5)
			love.graphics.line(start_x, start_y, x, y)
		love.graphics.setCanvas()
		
		start_x, start_y = nil, nil
	end
end
yx961031
Prole
Posts: 6
Joined: Sat Dec 10, 2016 4:57 am

Re: How draw a line

Post by yx961031 »

....
It is important that read wiki two times.
Though I know it is hard to do.
Post Reply

Who is online

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