Code archives/Algorithms/- Line Helpers -

This code has been declared by its author to be Public Domain code.

Download source code

- Line Helpers - by Jeremy Alessi2004
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.
;====== 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.

Code Archives Forum