2d normals of a 2d line

Started by RemiD, November 27, 2022, 09:51:02

Previous topic - Next topic

RemiD


;2d normals of a 2d line example by RemiD (20221127)
;calculates the 2 perpendicular normals of a 2d line, using the crossproduct and inversed crossproduct

gwidth% = 640 : gheight% = 480
Graphics3D(gwidth,gheight,32,2)

SeedRnd(MilliSecs())

Goto ChooseLineProperties

Repeat

If( MouseHit(1)=1 Or KeyHit(57)=1 )

.ChooseLineProperties

  ;A and B are the points of the 2D line, C and D are the points of the other vector forming the plane in order to calculate the cross product
  pA_x# = Rand(-640+1,640-1) : pA_y# = Rand(-480+1,480-1) : pA_z# = 0
  pB_x# = Rand(-640+1,640-1) : pB_y# = Rand(-480+1,480-1) : pB_z# = 1
  pC_x# = pA_x : pC_y# = pA_y : pC_z# = 0
  pD_x# = pA_x : pD_y# = pA_y  : pD_z# = 1

  ;vectorAB
  vAB_x# = pB_x - pA_x : vAB_y# = pB_y - pA_y : vAB_z# = pB_z - pA_z
  ;normalizedvectorAB
  vAB_length# = Sqr( ( vAB_x * vAB_x ) + ( vAB_y * vAB_y ) + ( vAB_z * vAB_z ) )
  nAB_x# = vAB_x / vAB_length : nAB_y# = vAB_y / vAB_length : nAB_z# = vAB_z / vAB_length

  ;vectorCD
  vCD_x# = pD_x - pC_x : vCD_y# = pD_y - pC_y : vCD_z# = pD_z - pC_z
  ;normalizedvectorCD
  vCD_length# = Sqr( ( vCD_x * vCD_x ) + ( vCD_y * vCD_y ) + ( vCD_z * vCD_z ) )
  nCD_x# = vCD_x / vCD_length : nCD_y# = vCD_y / vCD_length : nCD_z# = vCD_z / vCD_length

  ;cross product vector, perpendicular to the two others vectors
  cp_x# = ( nAB_y * nCD_z ) - ( nAB_z * nCD_y )  : cp_y# = ( nAB_z * nCD_x ) - ( nAB_x * nCD_z )  : cp_z# = ( nAB_x * nCD_y ) - ( nAB_y * nCD_x )
  ;inversed cross product vector, perpendicular to the two others vectors
  pc_x# = ( nAB_z * nCD_y ) - ( nAB_y * nCD_z )  : pc_y# = ( nAB_x * nCD_z ) - ( nAB_z * nCD_x )  : pc_z# = ( nAB_y * nCD_x ) - ( nAB_x * nCD_y )

EndIf

SetBuffer(BackBuffer())
Color(000,000,000) : Cls()

;draw vectorAB
Color(240,240,240) : Line( gwidth/2, gheight/2, gwidth/2+nAB_x*100, gheight/2+nAB_y*100)

;draw vectorCD
Color(120,120,120) : Line( gwidth/2, gheight/2, gwidth/2+nCD_x*100, gheight/2+nCD_y*100 )

;draw cross product vector
Color(240,000,240) : Line( gwidth/2, gheight/2, gwidth/2+cp_x*100, gheight/2+cp_y*100 )
;draw inversed cross product vector
Color(000,240,240) : Line( gwidth/2, gheight/2, gwidth/2+pc_x*100, gheight/2+pc_y*100 )

Flip()

Until( KeyDown(1)=1 )

End()

STEVIE G

Crossproduct is not required in 2d assuming Z coordinate is 0. The x,y normals are -nABy, nABx and nABy, -nABx.

RemiD

Quoteassuming Z coordinate is 0. The x,y normals are -nABy, nABx and nABy, -nABx.

this seems to work indeed. thanks !

what is the name of this math formula ?