Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drawing a line...
#1
Does anyone know how I would draw a line without the LINE command. I know it must use the SIN and COS command, but I could be wrong. Hopefully it's not the difficult, because it certainly sounds easy enough...
Reply
#2
Define a slope (call it SLOPE) and y-intercept (call it INTERCEPT), pick a value for an attribute (call it ATTR), and for X between X1 and X2:

FOR X = X1 TO X2
Y = SLOPE * X + INTERCEPT
PSET (X,Y), ATTR
NEXT X
ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
Reply
#3
I've got a tutorial that explains a very fast method using simple addition and subtraction - should I email it to you?
Reply
#4
Bresenham, eh?

Code:
SUB DrawLine (x1, y1, x2, y2, colr)

  IF y1 > y2 THEN
    SWAP y1, y2
    SWAP x1, x2
  END IF

  DeltaX = x2 - x1
  DeltaY = y2 - y1

  IF DeltaX > 0 THEN
  
    IF DeltaX > DeltaY THEN
      DeltaYx2 = DeltaY * 2
      DeltaYx2MinusDeltaXx2 = DeltaYx2 - (DeltaX * 2)
      ErrorTerm = DeltaYx2 - DeltaX
      PSET (x1, y1), colr
      DO WHILE DeltaX > 0
        DeltaX = DeltaX - 1
        IF ErrorTerm >= 0 THEN
          y1 = y1 + 1
          ErrorTerm = ErrorTerm + DeltaYx2MinusDeltaXx2
        ELSE
          ErrorTerm = ErrorTerm + DeltaYx2
        END IF
        x1 = x1 + 1
        PSET (x1, y1), colr
      LOOP
    ELSE
      DeltaXx2 = DeltaX * 2
      DeltaXx2MinusDeltaYx2 = DeltaXx2 - (DeltaY * 2)
      ErrorTerm = DeltaXx2 - DeltaY
      PSET (x1, y1), colr
      DO WHILE DeltaY > 0
        DeltaY = DeltaY - 1
        IF ErrorTerm >= 0 THEN
          x1 = x1 + 1
          ErrorTerm = ErrorTerm + DeltaXx2MinusDeltaYx2
        ELSE
          ErrorTerm = ErrorTerm + DeltaXx2
        END IF
        y1 = y1 + 1
        PSET (x1, y1), colr
      LOOP
    END IF

  ELSE
      
    DeltaX = -DeltaX
    IF DeltaX > DeltaY THEN
      DeltaYx2 = DeltaY * 2
      DeltaYx2MinusDeltaXx2 = DeltaYx2 - (DeltaX * 2)
      ErrorTerm = DeltaYx2 - DeltaX
      PSET (x1, y1), colr
      DO WHILE DeltaX > 0
        DeltaX = DeltaX - 1
        IF ErrorTerm >= 0 THEN
          y1 = y1 + 1
          ErrorTerm = ErrorTerm + DeltaYx2MinusDeltaXx2
        ELSE
          ErrorTerm = ErrorTerm + DeltaYx2
        END IF
        x1 = x1 - 1
        PSET (x1, y1), colr
      LOOP
    ELSE
      DeltaXx2 = DeltaX * 2
      DeltaXx2MinusDeltaYx2 = DeltaXx2 - (DeltaY * 2)
      ErrorTerm = DeltaXx2 - DeltaY
      PSET (x1, y1), colr
      DO WHILE DeltaY > 0
        DeltaY = DeltaY - 1
        IF ErrorTerm >= 0 THEN
          x1 = x1 - 1
          ErrorTerm = ErrorTerm + DeltaXx2MinusDeltaYx2
        ELSE
          ErrorTerm = ErrorTerm + DeltaXx2
        END IF
        y1 = y1 + 1
        PSET (x1, y1), colr
      LOOP
    END IF

  END IF

END SUB
Reply
#5
Glenn: Could you show me how I would do that in a sub please? With the varibles and stuff?

Plasma: *brain shorts out* *lungs melt away* *alarms flash on and off* *dies*
Reply
#6
I'll assume you ultimately want to work with screen coordinates. (I really wish I'd have read that before drinking the undiluted amaretto. Oh, boy. Here goes nothing.)

SUB DRAWLINE(X1 AS INTEGER, X2 AS INTEGER, SLOPE AS SINGLE, INTERCEPT AS SINGLE, ATTR AS INTEGER)
DIM X AS INTEGER, Y AS INTEGER
FOR X = X1 TO X2
Y = SLOPE * X + INTERCEPT
PSET (X,Y), ATTR
NEXT X
END SUB


*Hick* up. (Do not construe this as a recommendation of something better or more useful than Bresenham's algorithm. It's just an implementation of the standard definition of a line. There is sometimes a difference between mathematics and what works "best" when a computer is involved.) Good night.

(If that actually works and you come to the conclusion that the amaretto obviously wasn't affecting anything, well, I feel obligated to point out that I had to come back and fix the SUB statement.) *Plop*
ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
Reply
#7
Glenn: I bet Mega wanted you to explain how to get...

1. Slope
2. the y-intercept.

;*)
As in the Linear Equation: y=mx+b and its different flavors.

Mega: you could look it up on an algebra book. ;*)
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#8
depends on what he's doing and what information he has. Math textbooks, for example, have a habit of simply specifying a slope and intercept in a homework problem to draw the line.

If you know that the points (x1,y1) and (x2,y2) lie on a (non-vertical) line,

SLOPE = (Y2 - Y1) / (X2 - X1)

and

Y-INTERCEPT = Y1 - SLOPE * X1 = Y2 - SLOPE * X2

If, alternatively, you know that the line makes an angle THETA with the x-axis and passes through the point (X1,Y1),

SLOPE = TAN(THETA)

and

Y-INTERCEPT = Y1 - SLOPE * X1, as before.
ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
Reply
#9
Mega, you reading this?

;*)
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#10
Yeah I'm readin' it... I get it, but I'm wondering how can I draw a line the supports X1, Y1 to X2,Y2...
Edit: *See's is* Alright, thanks guys.,.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)