Program CohenSutherlandClipping Define ABOVE As Integer = 8 // Set Bit 4 Define BELOW As Integer = 4 // Set Bit 3 Define RIGHT As Integer = 2 // Set Bit 2 Define LEFT As Integer = 1 // Set Bit 1 Define Outside As Boolean Define test As BitFiddle // test addition to surface library, "And_"... // Create some lines and clipp it Method Main() SetScreenSize( 640, 480 ) Define Box As Rectangle Box.Color = DarkBlue Box.X = 100 Box.Y = 100 Box.Width = 440 Box.Height = 280 Define MyLine As Line Define Counter As Integer While Not IsKeyDown( Escape ) Counter = 0 ClearBackground( Black ) While Counter < 50 MyLine.StartPoint.X = Random( 10, ScreenWidth()-20) MyLine.StartPoint.Y = Random( 10, ScreenHeight()-20) MyLine.EndPoint.X = Random( 10, ScreenWidth()-20) MyLine.EndPoint.Y = Random( 10, ScreenHeight()-20) MyLine.Color = Red MyLine.Draw() Box.Draw() ClipLine( Box, MyLine ) If Not Outside Then MyLine.Color = Yellow MyLine.Draw() Box.Draw() End If Delay( 300 ) Counter = Counter + 1 End While End While End Method Function ClipLine( Box As Rectangle, ClipLine As Line ) As Line Define Code1 As Integer Define Code2 As Integer Define Dx As Decimal = ClipLine.EndPoint.X - ClipLine.StartPoint.X Define Dy As Decimal = ClipLine.EndPoint.Y - ClipLine.StartPoint.Y Define Done As Boolean = False While Not Done Code1 = FindRegion( Box, ClipLine.StartPoint ) Code2 = FindRegion( Box, ClipLine.EndPoint ) If TEST.OR_( Code1, Code2 ) = 0 Then // Line is in Box Outside = False Return ClipLine Else If Test.And_( Code1, Code2 ) <> 0 Then // Line is Complete outside Outside = True Return ClipLine Else If Test.And_( Code1, LEFT ) = LEFT Then // Startpoint is left out ClipLine.StartPoint.Y = ClipLine.StartPoint.Y + ( Box.Left - ClipLine.StartPoint.X ) * Dy / Dx ClipLine.StartPoint.X = Box.Left Else If Test.And_( Code2, LEFT ) = LEFT Then // Endpoint is left out ClipLine.EndPoint.Y = ClipLine.EndPoint.Y + ( Box.Left - ClipLine.EndPoint.X ) * Dy / Dx ClipLine.EndPoint.X = Box.Left Else If Test.And_( Code1, RIGHT ) = RIGHT Then // Startpoint is right out ClipLine.StartPoint.Y = ClipLine.StartPoint.Y + ( Box.Right - ClipLine.StartPoint.X ) * Dy / Dx ClipLine.StartPoint.X = Box.Right Else If Test.And_( Code2, RIGHT ) = RIGHT Then // Endpoint is right out ClipLine.EndPoint.Y = ClipLine.EndPoint.Y + ( Box.Right - ClipLine.EndPoint.X ) * Dy / Dx ClipLine.EndPoint.X = Box.Right Else If Test.And_( Code1, BELOW ) = BELOW Then // Startpoint is below out ClipLine.StartPoint.X = ClipLine.StartPoint.X + ( Box.Bottom - ClipLine.StartPoint.Y ) * Dx / Dy ClipLine.StartPoint.Y = Box.Bottom Else If Test.And_( Code2, BELOW ) = BELOW Then // Endpoint is below out ClipLine.EndPoint.X = ClipLine.EndPoint.X + ( Box.Bottom - ClipLine.EndPoint.Y ) * Dx / Dy ClipLine.EndPoint.Y = Box.Bottom Else If Test.And_( Code1, ABOVE ) = ABOVE Then // Startpoint is above out ClipLine.StartPoint.X = ClipLine.StartPoint.X + ( Box.Top - ClipLine.StartPoint.Y ) * Dx / Dy ClipLine.StartPoint.Y = Box.Top Else If Test.And_( Code2, ABOVE ) = ABOVE Then // Endpoint is above out ClipLine.EndPoint.X = ClipLine.EndPoint.X + ( Box.Top - ClipLine.EndPoint.Y ) * Dx / Dy ClipLine.EndPoint.Y = Box.Top End If End While Return ClipLine End Function Function FindRegion( Box As Rectangle, ClipPoint As Point ) As Integer Define Code As Integer = 0 If ClipPoint.X < Box.Left Then Code = Code + LEFT Else If ClipPoint.X > Box.Right Then Code = Code + RIGHT End If If ClipPoint.Y < Box.Top Then Code = Code + ABOVE Else If ClipPoint.Y > Box.Bottom Then Code = Code + BELOW End If Return Code End Function // -------------------------------------------------------------- // 1 OR 1 = 1 // 1 OR 0 = 1 // 0 OR 1 = 1 // 0 OR 0 = 0 // -------------------------------------------------------------- Function OR_( a As Integer, b As Integer ) As Integer Define a1 As Integer Define b1 As Integer Define c As Integer = 0 Define d As Integer = 1 While Not ( a = 0 And b = 0 ) a1 = a a = a / 2 b1 = b b = b / 2 If a1 <> a + a Or b1 <> b + b Then c = c + d End If d = d + d End While Return c End Function // -------------------------------------------------------------- // 1 AND 1 = 1 // 1 AND 0 = 0 // 0 AND 1 = 0 // 0 AND 0 = 0 // -------------------------------------------------------------- Function AND_( a As Integer, b As Integer ) As Integer Define a1 As Integer Define b1 As Integer Define c As Integer = 0 Define d As Integer = 1 While Not ( a = 0 Or b = 0 ) a1 = a a = a / 2 b1 = b b = b / 2 If a1 <> a + a And b1 <> b + b Then c = c + d End If d = d + d End While Return c End Function // -------------------------------------------------------------- // 1 EOR 1 = 0 // 1 EOR 0 = 1 // 0 EOR 1 = 1 // 0 EOR 0 = 0 // -------------------------------------------------------------- Function EOR_( a As Integer, b As Integer ) As Integer Define a1 As Integer Define b1 As Integer Define c As Integer = 0 Define d As Integer = 1 While Not ( a = 0 And b = 0 ) a1 = a a = a / 2 b1 = b b = b / 2 If ( a1 <> a + a And b1 = b + b ) Or ( a1 = a + a And b1 <> b + b ) Then c = c + d End If d = d + d End While Return c End Function End Program