Author Topic: Using Bresenham's line algorithm to create straight lines in Minecraft  (Read 2296 times)

milesluigi

  • Miles Luigiworth
  • Administrator
  • Hero Member
  • *****
  • Posts: 551
    • Techyoshi.com
I decided to write a quick JavaScript based html page using Bresenham's line algorithm. You can use if you want to create straight lines in three dimensional space (technically, any dimensional space. Minecraft is 3d.) It's useful if you want to make a Dodecahedron like me, or if you just need a straight line made out of blocks. Especially useful if you need to make a line that is not parallel to any three dimensional axis.

Copy the text, paste it into notepad, and save the file as a *.html file. Open it up in a web browser. Edit the x1,y1,z1 and x2,y2,z2 values to correspond to your line-creation needs.

Code: [Select]
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

  <title>Line Draw</title>
  <meta name="description" content="">
  <meta name="author" content="">

  <meta name="viewport" content="width=device-width,initial-scale=1">

</head>

<body>
<script type="text/javascript">
var x1=-1150;
var x2=-1178;
var y1=96;
var y2=102;
var z1=198;
var z2=206;

var p_x=x1; var p_y=y1; var p_z=z1;
var d_x=x2-x1; var d_y=y2-y1; var d_z=z2-z1;
var N=Math.max(Math.abs(d_x),Math.abs(d_y),Math.abs(d_z));
var s_x=d_x/N; var s_y=d_y/N; var s_z=d_z/N;

document.write(x1.toString()+","+y1.toString()+","+z1.toString()+"</br>");
for (var ii=1;ii<=N;ii++){
p_x+=s_x; p_y+=s_y; p_z+=s_z;
document.write(Math.round(p_x).toString()+","+Math.round(p_y).toString()+","+Math.round(p_z).toString()+"</br>");
}
</script>
</body>
</html>