line in standard form

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

line in standard form

Post by dafhi »

Code: Select all

/' -- standard form Line plotter - 2018 Aug 26 update 1 - by dafhi --

  I was experiencing slowness trying to visualize an intersection,
  so I decided to investigate lines in standard form:
 
  Ax + By = C
 
  If you're driven by curiosity, you might like

  update:  changed constructor to A,B,C rather than .get_dims(w, h)

'/



'includes?!  We don't need no stinking includes!!!

type myint as integer
type float as single


type LineInStandardForm
   
    /' -- standard form:  Ax + By = C
       
        First, window dimensions
      dim as LineInStandardForm L:  L.get_dims(w, h)
        Second,
      L.plot A, B, C, RGB(rr,gg,bb)
    '/
   
    as float            A,B,C
   
    declare function    x(as float) as float
    declare function    y(as float) as float
    declare sub         get_dims(as myint, as myint)
    declare sub         plot(as float, as float, as float, as ulong = RGB(255,255,255))
    declare property    steep as boolean
    declare constructor (as float=0, as float=0, as float=0)
   private:
    as myint            w, h
    as float            wh, hh
End Type
constructor.LineInStandardForm(_a as float, _b as float, _c as float)
    a=_a: b=_b: c=_c
End Constructor
property LineInStandardForm.steep as boolean
    return a*a > b*b
End Property
sub LineInStandardForm.get_dims(_w as myint, _h as myint)
    w=_w: wh = w/2
    h=_h: hh = h/2
End Sub
function LineInStandardForm.x(_y as float) as float
    return (c - b*_y) / a
End Function
function LineInStandardForm.y(_x as float) as float
    return (c - a*_x) / b
End Function
sub LineInStandardForm.plot(_a as float, _b as float, _c as float, col as ulong)
    a=_a:  b=_b:  c=_c
    if steep then
      for i as myint = -hh to h-1 - hh
        pset ( this.x(i) + wh, i + hh ), col
      Next
    else
      for i as myint = -wh to w-1 - wh
        pset ( i + wh, this.y(i) + hh ), col
      Next
    EndIf
End Sub


sub draw_axes(w as myint, h as myint, grid as myint = 10)
    var wh = w/2, wm = w - 1
    var hh = h/2, hm = h - 1
    line (wh,  0)-(wh, hm), rgb(60,110,120) 'V
    line (0,  hh)-(wm, hh), rgb(60,110,120) 'H
    for y as myint = 0 to hh step grid
      for x as myint = 0 to wh step grid
        pset (wh + x, hh + y)
        pset (wh - x, hh + y)
        pset (wh + x, hh - y)
        pset (wh - x, hh - y)
      Next
    Next
End Sub


sub main
   
    var w = 640
    var h = 480
   
    screenres w, h, 32
 
    dim as LineInStandardForm   L:  L.get_dims w,h
   
    draw_axes w, h
   
    L.plot 0, 25, 0, rgb(0,255,255)
   
    sleep
   
End Sub


main
Last edited by dafhi on Aug 27, 2018 13:50, edited 1 time in total.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: line in standard form

Post by fxm »

Joke:
You have not taken into account the case where conditions A = 0, B = 0, C = 0 are checked simultaneously.
In that case, the graphic solution corresponds to all points of the plan! :-)
dafhi
Posts: 1640
Joined: Jun 04, 2005 9:51

Re: line in standard form

Post by dafhi »

I didn't understand at first because it's been so long since I've worked at this level.

A more correct version:

Code: Select all

for y ..
  for x ..
    if abs(a*x + b*y - c) < .000001 then pset (x,y)
It's aliiiiive!
Post Reply