C#.Net写的Juliet集程序
选了一门物理学的课,老师留的作业,试着用C#写了下。分享下程序代码和大家交流一下。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 复数类
public class Complex
{
//默认构造函数
public Complex()
: this(0, 0)
{
}
// 只有实部的构造函数
public Complex(double real)
: this(real, 0)
{
}
// 由实部和虚部构造
public Complex(double real, double image)
{
this.real = real;
this.image = image;
}
private double real;
public double Real
{
get { return real; }
set { real = value; }
}
private double image;
// 复数的虚部
public double Image
{
get { return image; }
set { image = value; }
}
//重载加法
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.image + c2.image);
}
//重载减法
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex(c1.real - c2.real, c1.image - c2.image);
}
//重载乘法
public static Complex operator *(Complex c1, Complex c2)
{
return new Complex(c1.real * c2.real - c1.image * c2.image, c1.image * c2.real + c1.real * c2.image);
}
}
private void button1_Click(object sender, EventArgs e)
{
int nColor;
int nRed;
int nGreen;
int nBlue;
double cx;
double cy;
cx = Convert.ToDouble(textBox1.Text);
cy = Convert.ToDouble(textBox2.Text);
Complex Zn = new Complex(0,0);
Complex C = new Complex(cx, cy);
for (int i = -300; i <= 300; i++)
{
for (int j = -200; j <= 200; j++)
{
Zn.Real = ((double)i)/200;
Zn.Image = ((double)j) / 200;
for (nColor = 0; nColor <= 214; nColor++)
{
if ((Zn.Real * Zn.Real + Zn.Image * Zn.Image) > 4)
{
break;
}
else
{
Zn = Zn * Zn + C;
}
}
//计算颜色RGB值
nRed = nColor / 36;
nGreen = (nColor - 36 * nRed)/6;
nBlue = nColor % 6;
//绘制图像
Graphics gra = this.pictureBox1.CreateGraphics();
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
Brush bush = new SolidBrush(Color.FromArgb(nRed*51, nGreen*51, nBlue*51));
gra.FillRectangle(bush, i + 300, j + 200, 2, 2);
}
}
}
}
}