PDA

View Full Version : Line Algorithm


nolachrymose
07-14-2002, 10:18 PM
Right now I'm concentrating on line and shape algorithms, and am not getting far as usual :o. I figured I should start with the easiest and move to the harder - thus, I am starting with the line. I can't even get that working!

Problem is, I don't get any errors - I just get a blank page. My guess is that my algorithm is flawed, but I don't see how. Any help is greatly appreciated. Code is below.

<html>
<head>
<title>Line Algorithm</title>
<script type="text/javascript">
function putpixel(x,y,col) {
var newDiv=document.createElement("div");
newDiv.setAttribute("style","width: 1; height: 10;");
newDiv.style.left=x; newDiv.style.top=y; newDiv.style.backgroundColor=col;

document.body.appendChild(newDiv);
}
function Line(x1,y,x2,col) {
var pointBetween=Math.abs(x1-x2);
while(pointBetween>0) {
putpixel(x1++,y,col);
pointBetween--;
}
}

function init() { Line(20,20,60,"blue"); }
</script>
</head>
<body onload="init();">
</body>
</html>

jkd
07-14-2002, 11:14 PM
Not an answer, but have you ever considered SVG?

<line x1="0" y1="0" x2="1" y2="1"/>

Is all that is needed to create a line with equation:

y = x;

:)

nolachrymose
07-15-2002, 12:14 AM
I haven't thought of that yet. I'm actually better aquainted with VML, but I guess I could give SVG a shot.

Thanks!

Happy coding! :)

jkd
07-15-2002, 02:18 AM
Originally posted by nolachrymose
I'm actually better aquainted with VML

*pfft* VML - I personally don't see it uses anymore. Whatever you can accomplish with VML can be done (and more so with SVG), and SVG is a W3C standard, and the excellent Adobe SVG plugin exists.
(Not to mention Mozilla has a decent amount of native SVG support :)).