Page 1 of 1

How draw a line

Posted: Fri Dec 30, 2016 12:31 am
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 ?

Re: How draw a line

Posted: Fri Dec 30, 2016 1:38 am
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?

Re: How draw a line

Posted: Fri Dec 30, 2016 2:30 am
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

Re: How draw a line

Posted: Fri Dec 30, 2016 2:43 am
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.

Re: How draw a line

Posted: Fri Dec 30, 2016 2:46 am
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

Re: How draw a line

Posted: Fri Dec 30, 2016 5:45 am
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

Re: How draw a line

Posted: Fri Dec 30, 2016 5:16 pm
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

Re: How draw a line

Posted: Mon Jan 02, 2017 12:56 am
by yx961031
....
It is important that read wiki two times.
Though I know it is hard to do.