/***********************************************************
mandel.c -- Mandelbrot (マンデルブロート) 集合
***********************************************************/
#N88BASIC

Const M = 1000
Const XMAX = 640
Const YMAX = 400
Sub main()
Dim i As Long, j As Long, imax As Long, jmax As Long
Dim maxiter As Long, count As Long

Dim xmin, xmax, ymin, ymax, x, y, a, b, a2, b2, dx, dy

Dim color[M] As Long


Input "xmin = ";xmin
Input "xmax = ";xmax
Input "ymin = ";ymin
Input "ymax = ";ymax
Input "maxiter = ";maxiter

dx = xmax - xmin
dy = ymax - ymin

If dx <= 0 or dy <= 0 or maxiter <= 0 or maxiter > M Then
End
End If

If dx * YMAX > dy * XMAX Then
imax = XMAX: jmax = (XMAX * dy / dx + 0.5)
Else
imax = (YMAX * dx / dy + 0.5) : jmax = YMAX
End If

dx = dx / imax
dy = dy / jmax
color[0] = 0
j = 7

For i = maxiter To 1 Step -1
color[i] = j
If j > 1 Then j=j-1
Next

For i = 0 To imax
x = xmin + i * dx
For j = 0 To jmax
y = ymax - j * dy : a = x : b = y
a2 = a * a : b2 = b * b : count = maxiter
While (a2 + b2 <= 4 And count)
b = 2 * a * b - y : a = a2 - b2 - x
a2 = a * a : b2 = b * b : count = count - 1
Wend
Pset(i, j), color[count]
Next
Next
End Sub

main()