]> Drawing Pretty Lines

Drawing Pretty Lines

I want to redo my homepage and for that project I'd like to be able to do a bit of drawing. One simple way to do this is to use something like Flash, but I'd like to keep it in straight HTML if possible. I was thinking I'd make some tables and fill the cells. The problem is making pretty lines. This page is about that…


To start out with, I need some basic functions for making tables and filling cells…


The simplest method for drawing lines is to simply plot the line through the grid and any square that the line crosses, darken.

The math for this is pretty straightforward. First define a few things:

If you imagine R to be embedded in a grid there are eight possible squares that L could exit into.

sectors

Three of these (6, 7, 8) can be eliminated by guaranteeing that Pi is to the left of Pf. This is done by simply switching the two if Pix > Pfx

Define m = Δy Δx = Pfy - Piy Pfx - Pix and a grid space with lines every δx and δy units. A grid square can be identified as Si,j where the square is the space bordered by the four lines: x=iδx x= i+1 δx y=jδy y= j+1 δy or, in other words: xy iδx x< i+1 δx ; jδy y< j+1 δy

For a given point P, i= Px δx and j= Py δy

To draw my line, I need the set of all Si,j xy PiPf ¯ Si,j iδx x< i+1 δx ; jδy y< j+1 δy

I think it would be pretty easy to find iteratively… Let's work first with the case where m<0 and m since we can already eliminate 6-8 by chosing which point to call Pi this means the ray is going down and right. It will enter 3, 4 or 5 next. To figure out which, use m.

  1. Start off in Si,j i= Pix δx ; j= Piy δy
  2. If Pfx i+1 δx and Pfy j+1 δy then we are at the end of the line and can stop.
  3. Otherwise, find the intersection of the lines PiPf ¯ and x= i+1 δx which is i+1 δx Piy + m i+1 δx - Pix The important figure is: Piy + m i+1 δx - Pix = Iy and it's relationship to the bottom border of the rectangle, y= j+1 δy The next square is: { Si+1,j if Iy< j+1 δy Si,j+1 if Iy> j+1 δy S i+1, j+1 if Iy= j+1 δy
  4. Repeat

The thing is that I was planning on drawing lines by filling in chunks of tables, but I can do it with groups of dots much more easily. What would that look like?

Rotation: °
Translation: ,
Scale: ,