This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
je06's profile picture

Filled Triangles

Started by je06, 07 August 2015 - 08:14 PM
je06 #1
Posted 07 August 2015 - 10:14 PM
I've been searching around but I can't seem to be able to find how to draw filled triangles.
How would someone draw filled triangles on screen?

Also, how would you rotate and scale 3d points?
Edited on 07 August 2015 - 08:35 PM
ErwinOlie #2
Posted 07 August 2015 - 11:17 PM
Well, this probably not thé best way, but it is á way.
'p' contains the three corner-points.

p = {{ 3, 3 }, { 19, 8 }, { 12, 17 }}
x, y = term.getSize()
matrix = {}

for i = 1, x do
  matrix[i] = {}
  for j = 1, y do
	matrix[i][j] = 0
  end
end

function drawLine( startX, startY, endX, endY)
	local minX = math.min( startX, endX )
	if minX == startX then
		minY = startY
		maxX = endX
		maxY = endY
	else
		minY = endY
		maxX = startX
		maxY = startY
	end

	local xDiff = maxX - minX
	local yDiff = maxY - minY

	if xDiff > math.abs(yDiff) then
		local y = minY
		local dy = yDiff / xDiff
		for x=minX,maxX do
matrix[x][math.floor( y + 0.5 )] = 1
			y = y + dy
		end
	else
		local x = minX
		local dx = xDiff / yDiff
		if maxY >= minY then
			for y=minY,maxY do
  matrix[math.floor( x + 0.5 )][y] = 1
				x = x + dx
			end
		else
			for y=minY,maxY,-1 do
  matrix[math.floor( x + 0.5 )][y] = 1
				x = x - dx
			end
		end
	end
end

drawLine(p[1][1], p[1][2], p[2][1], p[2][2])
drawLine(p[1][1], p[1][2], p[3][1], p[3][2])
drawLine(p[2][1], p[2][2], p[3][1], p[3][2])

for i = 1, x do
  s, p1, p2 = 0, -1, -1
  for j = 1, y do
if matrix[i][j] == 1 then
if s == 0 then
   s = 1
p1 = j
elseif s == 2 then
   s = 3
p2 = j
end
else
if s == 1 then
   s = 2
	  end
end
if s == 3 then
for k = p1, p2 do
   matrix[i][k] =  1
end
break
end
  end
end

for i = 1, x do
  for j = 1, y do
	if matrix[i][j] == 1 then
	  term.setCursorPos(i, j)
	  write("#")
end
  end
end

I must say, that i have 'borrowed' a piece of the paintutils api, so also credits to the creator of it :)/>
H4X0RZ #3
Posted 08 August 2015 - 12:43 AM
I once made something like this in Scratch. It's not the best way but it works:

Define 3 positions. Choose 2 of them to be the "ground" and draw a line between them. Now slowly "move" from the first grond-point to the other and after each iteration draw a line from the current position to the third position.

You could modify .drawLine so you just get a table of coordinates instead of actually drwaing to "move" on the "ground" for example.
Bomb Bloke #4
Posted 08 August 2015 - 07:17 AM
I use this technique for basic poly-fills.

In terms of rotation, you need to pick a point to rotate around - you can then use basic trig to calculate the vectors between that and the points in your object (that is to say, figure out the length and angles of the lines between those points). It's then simply a matter of changing the angles of those vectors and noting the new end points.