;Midpoint ellipse algorithmGraphics 640,480,0,2Local xCenter% = 320Local yCenter% = 240Local Rx%, Ry%Local p%, px%, py%, x%, y%Local Rx2%, Ry2%, twoRx2%, twoRy2%Local pFloat#Repeat Cls WaitEvent() If MouseDown(1) xCenter = MouseX() yCenter = MouseY() EndIf Rx = Abs(xCenter - MouseX()) Ry = Abs(yCenter - MouseY()) Rx2 = Rx * Rx Ry2 = Ry * Ry twoRx2 = Rx2 Shl 1 twoRy2 = Ry2 Shl 1 msStart = MilliSecs() ;Region 1 x = 0 y = Ry Plot xCenter + x, yCenter + y Plot xCenter - x, yCenter + y Plot xCenter + x, yCenter - y Plot xCenter - x, yCenter - y pFloat = (Ry2 - (Rx2 * Ry)) + (0.25 * Rx2) p = Int(pFloat) If pFloat Mod 1.0 >= 0.5 Then p = p + 1 px = 0 py = twoRx2 * y While px < py x = x + 1 px = px + twoRy2 If p >= 0 y = y - 1 py = py - twoRx2 EndIf If p < 0 Then p = p + Ry2 + px Else p = p + Ry2 + px - py Plot xCenter + x, yCenter + y Plot xCenter - x, yCenter + y Plot xCenter + x, yCenter - y Plot xCenter - x, yCenter - y Wend ;Region 2 pFloat = (Ry2 * (x + 0.5) * (x + 0.5)) + (Rx2 * (y - 1.0) * (y - 1.0)) - (Rx2 * (Float(Ry2))) p = Int(pFloat) If pFloat Mod 1.0 >= 0.5 Then p = p + 1 While y > 0 y = y - 1 py = py - twoRx2 If p <= 0 x = x +1 px = px + twoRy2 EndIf If p > 0 Then p = p + Rx2 - py Else p = p + Rx2 - py + px Plot xCenter + x, yCenter + y Plot xCenter - x, yCenter + y Plot xCenter + x, yCenter - y Plot xCenter - x, yCenter - y Wend timeToDraw = MilliSecs() - msStart Text 20, 20, Rx + " x " + Ry Text 20, 30, "Draw time " + timeToDraw + " ms" Flip(False)Until KeyHit(1)
;Midpoint ellipse algorithmConst GAME_WIDTH = 800Const GAME_HEIGHT = 600Graphics GAME_WIDTH, GAME_HEIGHT, 0, 2Local xCenter% = GAME_WIDTH / 2Local yCenter% = GAME_HEIGHT / 2Local Rx%, Ry%Local p%, px%, py%, x%, y%Local Rx2%, Ry2%, twoRx2%, twoRy2%Local pFloat#Const fillColor% = $FFRepeat Cls WaitEvent() If MouseDown(1) xCenter = MouseX() yCenter = MouseY() EndIf Rx = Abs(xCenter - MouseX()) Ry = Abs(yCenter - MouseY()) Rx2 = Rx * Rx Ry2 = Ry * Ry twoRx2 = Rx2 Shl 1 twoRy2 = Ry2 Shl 1 msStart = MilliSecs() ;Region 1 x = 0 y = Ry LockBuffer(BackBuffer) wpf(xCenter + x, yCenter + y) wpf(xCenter - x, yCenter + y) wpf(xCenter + x, yCenter - y) wpf(xCenter - x, yCenter - y) pFloat = (Ry2 - (Rx2 * Ry)) + (0.25 * Rx2) p = Int(pFloat) If pFloat Mod 1.0 >= 0.5 Then p = p + 1 px = 0 py = twoRx2 * y While px < py x = x + 1 px = px + twoRy2 If p >= 0 y = y - 1 py = py - twoRx2 EndIf If p < 0 Then p = p + Ry2 + px Else p = p + Ry2 + px - py wpf(xCenter + x, yCenter + y) wpf(xCenter - x, yCenter + y) wpf(xCenter + x, yCenter - y) wpf(xCenter - x, yCenter - y) Wend ;Region 2 pFloat = (Ry2 * (x + 0.5) * (x + 0.5)) + (Rx2 * (y - 1.0) * (y - 1.0)) - (Rx2 * (Float(Ry2))) p = Int(pFloat) If pFloat Mod 1.0 >= 0.5 Then p = p + 1 While y > 0 y = y - 1 py = py - twoRx2 If p <= 0 x = x +1 px = px + twoRy2 EndIf If p > 0 Then p = p + Rx2 - py Else p = p + Rx2 - py + px wpf(xCenter + x, yCenter + y) wpf(xCenter - x, yCenter + y) wpf(xCenter + x, yCenter - y) wpf(xCenter - x, yCenter - y) Wend UnlockBuffer(BackBuffer) timeToDraw = MilliSecs() - msStart Text 20, 20, Rx + " x " + Ry Text 20, 30, "Draw time " + timeToDraw + " ms" Flip(False)Until KeyHit(1)Function wpf(x,y) ;safe WritePixelFast If x < 0 Or x >= GAME_WIDTH Then Return 0 If y < 0 Or y >= GAME_HEIGHT Then Return 0 WritePixelFast(x, y, fillColor%)End Function