Skip to content

Instantly share code, notes, and snippets.

@PiotrWegrzyn
Last active April 23, 2018 10:19
Show Gist options
  • Save PiotrWegrzyn/641f145512bf98893d4523ae37a3d22d to your computer and use it in GitHub Desktop.
Save PiotrWegrzyn/641f145512bf98893d4523ae37a3d22d to your computer and use it in GitHub Desktop.
C#;Grafika komputerowa;Krzywe B-Składane;April2018
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//best data set: 0,0 30,50 40,80 100,40
namespace GK_05_Krzywe
{
public partial class Form1 : Form
{
private System.Drawing.Graphics g;
private System.Drawing.Pen penBlue = new System.Drawing.Pen(Color.Blue, 2);
private System.Drawing.Pen penRed = new System.Drawing.Pen(Color.Red, 2);
private System.Drawing.Pen pen1 = new System.Drawing.Pen(Color.Black, 1);
private System.Drawing.Pen pen2 = new System.Drawing.Pen(Color.Red, 1);
private System.Drawing.Pen pen3 = new System.Drawing.Pen(Color.Blue, 1);
private System.Drawing.Brush br1 = new System.Drawing.SolidBrush(Color.Blue);
public Form1()
{
InitializeComponent();
g = pictureBox1.CreateGraphics();
}
private void button1_Click(object sender, EventArgs e)
{
int numberOfPoints = 1000;
int midX = pictureBox1.Width / 2, midY = pictureBox1.Height / 2, maxX = pictureBox1.Width, maxY = pictureBox1.Height;
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp);
PointF P1 = new PointF((float)Double.Parse(textBox1.Text), (float)Double.Parse(textBox2.Text));
PointF P2 = new PointF((float)Double.Parse(textBox3.Text), (float)Double.Parse(textBox4.Text)); //vector
PointF P3 = new PointF((float)Double.Parse(textBox5.Text), (float)Double.Parse(textBox6.Text)); //vettor
PointF P4 = new PointF((float)Double.Parse(textBox7.Text), (float)Double.Parse(textBox8.Text));
gr.DrawLine(pen3, midX + P1.X, midY - P1.Y, midX + P2.X + P1.X, midY - P2.Y - P1.Y);
gr.DrawLine(pen3, midX + P4.X, midY - P4.Y, midX + P3.X + P4.X, midY - P3.Y - P4.Y);
double t = 0;
PointF[] drawingPoints = new PointF[numberOfPoints];
for (int i = 0; i < numberOfPoints; i++)
{
drawingPoints[i].X = midX + (float)((2 * Math.Pow(t, 3) - 3 * Math.Pow(t, 2) + 1) * P1.X + (-2 * Math.Pow(t, 3) + 3 * Math.Pow(t, 2)) * P4.X + (Math.Pow(t, 3) - 2 * Math.Pow(t, 2) + t) * P2.X + (Math.Pow(t, 3) - Math.Pow(t, 2)) * P3.X);
drawingPoints[i].Y = midY - (float)((2 * Math.Pow(t, 3) - 3 * Math.Pow(t, 2) + 1) * P1.Y + (-2 * Math.Pow(t, 3) + 3 * Math.Pow(t, 2)) * P4.Y + (Math.Pow(t, 3) - 2 * Math.Pow(t, 2) + t) * P2.Y + (Math.Pow(t, 3) - Math.Pow(t, 2)) * P3.Y);
t += 1.0 / numberOfPoints;
}
gr.FillEllipse(br1, midX + P1.X - 2, midY - P1.Y - 2, 4, 4);
gr.FillEllipse(br1, midX + P4.X - 2, midY - P4.Y - 2, 4, 4);
gr.DrawLines(pen2, drawingPoints);
gr.DrawLine(pen1, 0, midY, maxX, midY);
gr.DrawLine(pen1, midX, 0, midX, maxY);
pictureBox1.Image = bmp;
}
private void button2_Click(object sender, EventArgs e)
{
int numberOfPoints = 1000;
int midX = pictureBox1.Width / 2, midY = pictureBox1.Height / 2, maxX = pictureBox1.Width, maxY = pictureBox1.Height;
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp);
PointF P1 = new PointF((float)Double.Parse(textBox1.Text), (float)Double.Parse(textBox2.Text));
PointF P2 = new PointF((float)Double.Parse(textBox3.Text), (float)Double.Parse(textBox4.Text));
PointF P3 = new PointF((float)Double.Parse(textBox5.Text), (float)Double.Parse(textBox6.Text));
PointF P4 = new PointF((float)Double.Parse(textBox7.Text), (float)Double.Parse(textBox8.Text));
double t = 0;
PointF[] drawingPoints = new PointF[numberOfPoints];
for (int i = 0; i < numberOfPoints; i++)
{
drawingPoints[i].X = midX + (float)(Math.Pow((1 - t), 3) * P1.X + 3 * t * (Math.Pow((1 - t), 2)) * P2.X + 3 * Math.Pow(t, 2) * (1 - t) * P3.X + Math.Pow(t, 3) * P4.X);
drawingPoints[i].Y = midY - (float)(Math.Pow((1 - t), 3) * P1.Y + 3 * t * (Math.Pow((1 - t), 2)) * P2.Y + 3 * Math.Pow(t, 2) * (1 - t) * P3.Y + Math.Pow(t, 3) * P4.Y);
t += 1.0 / numberOfPoints;
}
gr.FillEllipse(br1, midX + P1.X - 2, midY - P1.Y - 2, 4, 4);
gr.FillEllipse(br1, midX + P2.X - 2, midY - P2.Y - 2, 4, 4);
gr.FillEllipse(br1, midX + P3.X - 2, midY - P3.Y - 2, 4, 4);
gr.FillEllipse(br1, midX + P4.X - 2, midY - P4.Y - 2, 4, 4);
gr.DrawLines(pen2, drawingPoints);
gr.DrawLine(pen1, 0, midY, maxX, midY);
gr.DrawLine(pen1, midX, 0, midX, maxY);
pictureBox1.Image = bmp;
}
//hermiet
private void button3_Click(object sender, EventArgs e)
{
int numberOfPoints = 1000;
int midX = pictureBox1.Width / 2, midY = pictureBox1.Height / 2, maxX = pictureBox1.Width, maxY = pictureBox1.Height;
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp);
PointF[] p = new PointF[8];
p[0] = new PointF((float)Double.Parse(textBox1.Text), (float)Double.Parse(textBox2.Text));
p[1] = new PointF((float)Double.Parse(textBox3.Text), (float)Double.Parse(textBox4.Text));
p[2] = new PointF((float)Double.Parse(textBox5.Text), (float)Double.Parse(textBox6.Text));
p[3] = new PointF((float)Double.Parse(textBox7.Text), (float)Double.Parse(textBox8.Text));
p[4].X = 80;
p[4].Y = -20;
p[5].X = 100;
p[5].Y = 20;
p[6].X = 120;
p[6].Y = -20;
p[7].X = 140;
p[7].Y = 20;
PointF[][] points;
points = new PointF[5][];
for (int i = 0; i < 5; i++)points[i] = new PointF[numberOfPoints];
gr.DrawLine(pen1, 0, midY, maxX, midY);
gr.DrawLine(pen1, midX, 0, midX, maxY);
double t = 0;
for (int j = 3; j < 8; j++)
{
t = 0;
for (int i = 0; i < numberOfPoints; i++)
{
points[j - 3][i].X = midX + (float)(Math.Pow((1 - t), 3) / 6 * p[j - 3].X + (3 * Math.Pow(t, 3) - 6 * Math.Pow(t, 2) + 4) / 6 * p[j - 2].X + (-3 * Math.Pow(t, 3) + 3 * Math.Pow(t, 2) + 3 * t + 1) / 6 * p[j - 1].X + Math.Pow(t, 3) / 6 * p[j].X);
points[j - 3][i].Y = midY - (float)(Math.Pow((1 - t), 3) / 6 * p[j - 3].Y + (3 * Math.Pow(t, 3) - 6 * Math.Pow(t, 2) + 4) / 6 * p[j - 2].Y + (-3 * Math.Pow(t, 3) + 3 * Math.Pow(t, 2) + 3 * t + 1) / 6 * p[j - 1].Y + Math.Pow(t, 3) / 6 * p[j].Y);
t += 1.0 / numberOfPoints;
}
}
for (int i = 0; i < 5; i++)gr.DrawLines(pen2, points[i]);
for (int i = 0; i < 8; i++)gr.FillEllipse(br1, midX + p[i].X - 3, midY - p[i].Y - 3, 6, 6);
pictureBox1.Image = bmp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment