Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Equation For Specific Line Length Needed
#8
This is a similar problem to how I did my unit circle visualizer. I had the radius line follow my mouse position, but it had to stop at the circle diameter regardless of where the mouse was at. I used a vector normalization scheme. A mouse position is essentially a 2D position vector in the form (_MOUSEX, _MOUSEY) and in order to get a direction you would:

subtract the hand position (xh%, yh%)  from the mouse position (xm%, ym%) to get the direction vector (dx%, dy%) of the sword

dx% = xm% - xh%
dy% = ym% - yh%

next step is to get the distance (aka magnitude) of the direction vector, and the _HYPOT command does that handily

mag! = _HYPOT(dx%, dy%) ' I'd use a single precision for the magnitude

next you divide dx% and dy% each by the mag! to get a unit vector (ux!, uy!)  <<<notice the unit vector is a single, that's important.

ux! = dx% / mag! ' <<<be sure to trap any mag! value of zero to avoid a division by zero error.
uy! = dy% / mag!           that could happen when your mouse was right on the hand  (if mag!=0 then skip the draw)

The unit vector is simply the direction with a length of one, guaranteed if the above was done correctly. So now multiply each unit element by the length of the sword to get the sword point position (spx!, spy!)

spx! = ux! * 100
spy! = uy! * 100

Then you could just draw the line of the sword with a STEP

LINE (xh%, yh%)-STEP(spx!, spy!) ' the line will round the spx!, spy! to the nearest integer or you could use INT or CINT if desired

It worked like a charm for me. Several steps, but simple equations.


A quickie demo of the process..
Code: (Select All)
SCREEN _NEWIMAGE(1024, 512, 32)
xh% = 512: yh% = 256
swordlength% = 100
DO
    CLS
    WHILE _MOUSEINPUT: WEND
    dx% = _MOUSEX - xh%
    dy% = _MOUSEY - yh%
    mag! = _HYPOT(dx%, dy%)
    IF mag! = 0 THEN
        ux! = 0: uy! = 0
    ELSE
        ux! = dx% / mag! '
        uy! = dy% / mag! '
    END IF
    spx! = ux! * swordlength%
    spy! = uy! * swordlength%
    LINE (xh%, yh%)-STEP(spx!, spy!) '
    _LIMIT 100
    _DISPLAY
LOOP UNTIL _KEYDOWN(27)
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply


Messages In This Thread
RE: Equation For Specific Line Length Needed - by OldMoses - 08-17-2022, 05:05 AM



Users browsing this thread: 4 Guest(s)