[bb] - Line Helpers - by Jeremy Alessi [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:43

Previous topic - Next topic

BlitzBot

Title : - Line Helpers -
Author : Jeremy Alessi
Posted : 1+ years ago

Description : These functions can help you in numerous ways and I didn't see them here.  I used to them to interpolate a point at the edge of the screen for an off-screen object.  This way a player character would be pointing at something off screen but the player wouldn't get lost as they'd see a relative symbol at the edge of the screen.  I used it for a 3D implementation in a strange way, but they are general functions that apply across the board.

Code :
Code (blitzbasic) Select
;====== RETURNX ==========================================================
; == Use this to return an x# for any given y# and two other
; == points on a coordinate plane

Function ReturnedX#(y#, X1#, Y1#, X2#, Y2#)

Return ( ( y# - YIntercept( X1#, Y1#, X2#, Y2# ) ) / Slope#( X1#, Y1#, X2#, Y2# ) )

End Function

;=========================================================================

;====== RETURNY ==========================================================
; == Use this to return a y# for any given x# and two other
; == points on a coordinate plane

Function ReturnedY#(x#, X1#, Y1#, X2#, Y2#)

Return ( Slope#( X1#, Y1#, X2#, Y2# ) * x# + YIntercept( X1#, Y1#, X2#, Y2# ) )

End Function

;=========================================================================

;====== SLOPE ============================================================

Function Slope#(X1#, Y1#, X2#, Y2#)

m# = ( ( Y2# - Y1# ) / ( X2# - X1# ) )

If m#=0
Return .001 ;avoid infinity
Else
Return m#
EndIf

End Function

;=========================================================================

;====== YINTERCEPT =======================================================

Function YIntercept(X1#, Y1#, X2#, Y2#)

Return ( (-Slope#( X1#, Y1#, X2#, Y2# ) * X1#) + Y1#)

End Function

;=========================================================================


Comments : none...