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

Anti Aliasing?

Started by Xenthera, 28 October 2014 - 01:29 AM
Xenthera #1
Posted 28 October 2014 - 02:29 AM
Anti Aliased line.. only works on a black background. It's the thought that counts though, right?

(click image to enlarge)




pixelWithAlpha = function(x,y,a)  -- Pseudo Alpha channel. Only works with black background (for obvious reasons)
	  
		alphaStep = 255 / 4
	  
		if a > 255 - alphaStep then
			term.setBackgroundColor(colors.white)
		elseif a > 255 - alphaStep * 2 then
			term.setBackgroundColor(colors.lightGray)
		elseif a > 255 - alphaStep * 3 then
			term.setBackgroundColor(colors.gray)
		else
			term.setBackgroundColor(colors.black)
			return
		end	
	  
		term.setCursorPos(x,y)
		term.write(" ")
end
aaLine = function(x0, y0, x1, y1)
  
		dx = math.abs(x1-x0)
		if x0<x1 then sx = 1 else sx = -1 end
		dy = math.abs(y1-y0)
		if y0<y1 then sy = 1 else sy = -1 end
	  
		err = dx-dy					
	  
		if dx + dy == 0 then
			ed = 1
		else
			ed = math.sqrt(dx*dx+dy*dy)
		end
	  
		while true do									  
			pixelWithAlpha(x0,y0, 255 - (255 * math.abs(err-dx+dy)/ed));
			e2 = err
			x2 = x0
			if (2*e2 >= -dx) then							
				if (x0 == x1) then break end
				if (e2+dy < ed) then pixelWithAlpha(x0,y0+sy, 255 - (255*(e2+dy)/ed)) end
				err = err - dy;
				x0 = x0 + sx;
			end
			if (2*e2 <= dy) then								  
				if (y0 == y1) then break end
				if (dx-e2 < ed) then pixelWithAlpha(x2+sx,y0, 255 - (255*(dx-e2)/ed)) end
				err  = err + dx;
				y0 = y0 + sy;
			end
	  end
  end

term.setBackgroundColor(colors.black)
term.clear()

aaLine(0,0,10,19)
paintutils.drawLine(5,0,15,19,colors.white)

or

http://pastebin.com/BwbmMTmQ
Edited on 28 October 2014 - 04:11 PM
TurtleHunter #2
Posted 28 October 2014 - 02:33 AM
Is the monitor scale 0.5? it doesnt looks like it
Xenthera #3
Posted 28 October 2014 - 02:35 AM
It is.
oeed #4
Posted 28 October 2014 - 05:50 AM
Pretty neat, you could also look in to using characters rather than colours.
Bomb Bloke #5
Posted 28 October 2014 - 06:54 AM
"Characters other than spaces", I guess you mean?
oeed #6
Posted 28 October 2014 - 07:10 AM
"Characters other than spaces", I guess you mean?
Yea, kinda like this:
http://www.computercraft.info/forums2/index.php?/topic/11161-behold-i-give-you-fire-demo/
Lyqyd #7
Posted 28 October 2014 - 02:28 PM
Moved to Programs.
Xenthera #8
Posted 28 October 2014 - 03:59 PM
Moved to Programs.

I hadn't put it in programs because it's not a program, It's just two functions.
Saldor010 #9
Posted 28 October 2014 - 04:21 PM
Moved to Programs.

I hadn't put it in programs because it's not a program, It's just two functions.

Agreed. It would be better in APIs/Utilities or back in its home in General.
Lyqyd #10
Posted 28 October 2014 - 04:47 PM
It's kind of ambiguous. It's a demo with working code (similar to Gopher's fire demo), but not necessarily a complete program as written. Hmm. It makes more sense being in the Programs section, as it will be more easily searchable if someone is looking for code. General might be better for short-term attention and visibility, but Programs or a subsection thereof is better for long term ease of seachability and visibility. I'll move it once more, wherever you'd prefer (within reason! :P/> ), but then it stays where it's at. Your choice.
Xenthera #11
Posted 28 October 2014 - 05:04 PM
Heh, Don't worry about it Lyqyd. I'll just add a couple lines to make it runnable. :)/>