-
-
Save neetsdkasu/2b2cf6673d9d2d259ec4775ffb35650f to your computer and use it in GitHub Desktop.
ellipse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fsc --target:winexe --codepage:65001 Main.fs | |
module Main | |
type private Application = System.Windows.Forms.Application | |
type private DockStyle = System.Windows.Forms.DockStyle | |
type private FlowDirection = System.Windows.Forms.FlowDirection | |
type private FlowLayoutPanel = System.Windows.Forms.FlowLayoutPanel | |
type private Form = System.Windows.Forms.Form | |
type private Label = System.Windows.Forms.Label | |
type private PictureBox = System.Windows.Forms.PictureBox | |
type private Timer = System.Windows.Forms.Timer | |
type private Bitmap = System.Drawing.Bitmap | |
type private Color = System.Drawing.Color | |
type private Graphics = System.Drawing.Graphics | |
type private Point = System.Drawing.Point | |
type private Size = System.Drawing.Size | |
type private SolidBrush = System.Drawing.SolidBrush | |
let private rand = new System.Random() | |
let private r = 100 | |
let private ps = | |
let th = rand.NextDouble() * 3.14 | |
let d = 70.0 | |
let x0 = d * cos th |> floor |> int | |
let y0 = d * sin th |> floor |> int | |
seq { | |
for y in -r..r do | |
for x in -r..r do | |
yield (x, y) | |
} | |
|> Seq.filter (fun (x, y) -> | |
y * x0 = y0 * x && x * x + y * y <= r * r | |
) | |
|> Seq.map (fun (x, y) -> new Point(x + r + 20, y + r + 20)) | |
|> Seq.toArray | |
let private idx = ref 0 | |
let private col = ref true | |
let private isin (p: Point) x y = | |
let (x0, y0) = p.X - 20, p.Y - 20 | |
let (x, y) = x - 20, y - 20 | |
let (dx1, dy1) = x0 - x, y0 - y | |
let (dx2, dy2) = r * 2 - x0 - x, r * 2 - y0 - y | |
let (dt1, dt2) = dx1 * dx1 + dy1 * dy1, dx2 * dx2 + dy2 * dy2 | |
let r1 = double dt1 |> sqrt | |
let r2 = double dt2 |> sqrt | |
r1 + r2 <= double (r * 2) | |
let private draw (picbox: PictureBox) (bitmap: Bitmap) = | |
use br = new SolidBrush(Color.Blue) | |
let c = if !col then Color.Black else Color.White | |
let p = Array.get ps !idx | |
for y in 20..20+r*2 do | |
for x in 20..20+r*2 do | |
if isin p x y then | |
bitmap.SetPixel(x, y, c) | |
else | |
bitmap.SetPixel(x, y, picbox.BackColor) | |
use g = Graphics.FromImage(bitmap) | |
g.FillEllipse(br, p.X - 5, p.Y - 5, 10, 10) | |
g.FillEllipse(br, r * 2 + 40 - p.X - 5, r * 2 + 40 - p.Y - 5, 10, 10) | |
picbox.Refresh() | |
idx := (!idx + 1) % Array.length ps | |
if !idx = 0 then | |
col := not !col | |
type private Form1() as this = | |
inherit Form(ClientSize = new Size(292, 266), | |
Text = "Draw") | |
let components = new System.ComponentModel.Container() | |
let timer1 = new Timer(components, | |
Interval = 200) | |
let flowlp = new FlowLayoutPanel(Dock = DockStyle.Fill, | |
FlowDirection = FlowDirection.TopDown) | |
let bitmap = new Bitmap(250, 250) | |
let picbox = new PictureBox(Image = bitmap, | |
Size = bitmap.Size) | |
let label = new Label() | |
do | |
flowlp.Controls.Add(picbox) | |
flowlp.Controls.Add(label) | |
this.Controls.Add(flowlp) | |
Event.add this.timer1_Timer timer1.Tick | |
member this.timer1_Timer _ = | |
let time0 = System.Environment.TickCount | |
draw picbox bitmap | |
let time1 = System.Environment.TickCount | |
let diff = time1 - time0 | |
label.Text <- sprintf "%d" diff | |
override this.OnLoad(e) = | |
base.OnLoad(e) | |
timer1.Start() | |
override this.Dispose(disposing) = | |
if disposing then | |
match components with | |
| null -> () | |
| _ -> components.Dispose() | |
base.Dispose(disposing) | |
[<System.STAThread()>] | |
do | |
Application.Run(new Form1()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment