Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 mousehandling
- {
- public partial class Form1 : Form
- {
- Graphics g;
- List<PointF> points;
- public Form1()
- {
- InitializeComponent();
- g = CreateGraphics();
- points = new List<PointF>();
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
- }
- private void Form1_MouseClick(object sender, MouseEventArgs e)
- {
- points.Add(new PointF(e.X, e.Y));
- DrawPoints();
- }
- private void DrawPoints()
- {
- Invalidate();
- Update();
- foreach (var point in points)
- {
- g.DrawEllipse(Pens.Black, point.X, point.Y, 10, 10);
- g.FillEllipse(Brushes.Red, point.X, point.Y, 10, 10);
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- Kirajzolas();
- }
- catch
- {
- MessageBox.Show("Legalább két pontot tegyél!");
- }
- }
- private void Kirajzolas()
- {
- Invalidate();
- Update();
- if (points.Count < 2)
- {
- throw new Exception("Kevés pont!");
- }
- if (checkBox1.Checked) DrawPoints();
- g.DrawCurve(new Pen(Color.Blue, 3), points.ToArray());
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Invalidate();
- Update();
- points.Clear();
- }
- //Kiegészítés trackbarokkal
- private TrackBar trackBar1;
- private TrackBar trackBar2;
- private void trackBar1_Scroll(object sender, EventArgs e)
- {
- novekmeny = trackBar1.Value - elozoX;
- elozoX = trackBar1.Value;
- eltolX(novekmeny);
- DrawPoints();
- DrawCurve();
- }
- private void eltolX(int novekmeny)
- {
- for (int i = 0; i < points.Count; i++)
- {
- points[i] = new PointF(points[i].X + novekmeny, points[i].Y);
- }
- }
- private void trackBar2_Scroll(object sender, EventArgs e)
- {
- novekmeny = -trackBar2.Value - elozoY;
- elozoY = -trackBar2.Value;
- eltolY(novekmeny);
- DrawPoints();
- DrawCurve();
- }
- private void eltolY(int novekmeny)
- {
- for (int i = 0; i < points.Count; i++)
- {
- points[i] = new PointF(points[i].X, points[i].Y + novekmeny);
- }
- }
- // Forgatás (NE MÁSOLD, ÁTNEVEZTEM FÜGGVÉNYEKET ÉS VÁLTOZÓKAT, magyarról angolra)
- private TrackBar trackBar3;
- float prevAlpha;
- private void trackBar3_Scroll(object sender, EventArgs e)
- {
- float alpha = (float)((trackBar3.Value - prevAlpha) * Math.PI / 180.0); // és így lett radián!
- prevAlpha = trackBar3.Value;
- Rotate(alpha);
- }
- private void Rotate(float alpha)
- {
- float newX, newY;
- for (int i = 0; i < points.Count; i++)
- {
- int centerX = (int)points[points.Count / 2].X;
- int centerY = (int)points[points.Count / 2].Y;
- moveX(-centerX);
- moveY(-centerY);
- newX = (float)(points[i].X * Math.Cos(alpha) - points[i].Y * Math.Sin(alpha));
- newY = (float)(points[i].X * Math.Sin(alpha) + points[i].Y * Math.Cos(alpha));
- points[i] = new PointF(newX, newY);
- moveX(centerX);
- moveY(centerY);
- DrawPoints();
- DrawCurve();
- }
- }
- // Designer kód
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.button1 = new System.Windows.Forms.Button();
- this.button2 = new System.Windows.Forms.Button();
- this.checkBox1 = new System.Windows.Forms.CheckBox();
- this.SuspendLayout();
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(12, 12);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(75, 23);
- this.button1.TabIndex = 0;
- this.button1.Text = "Draw";
- this.button1.UseVisualStyleBackColor = true;
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // button2
- //
- this.button2.Location = new System.Drawing.Point(12, 41);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(75, 23);
- this.button2.TabIndex = 1;
- this.button2.Text = "Reset";
- this.button2.UseVisualStyleBackColor = true;
- this.button2.Click += new System.EventHandler(this.button2_Click);
- //
- // checkBox1
- //
- this.checkBox1.AutoSize = true;
- this.checkBox1.Checked = true;
- this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
- this.checkBox1.Location = new System.Drawing.Point(12, 70);
- this.checkBox1.Name = "checkBox1";
- this.checkBox1.Size = new System.Drawing.Size(87, 17);
- this.checkBox1.TabIndex = 2;
- this.checkBox1.Text = "Visible points";
- this.checkBox1.UseVisualStyleBackColor = true;
- //
- // Form1
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(691, 366);
- this.Controls.Add(this.checkBox1);
- this.Controls.Add(this.button2);
- this.Controls.Add(this.button1);
- this.Name = "Form1";
- this.Text = "Form1";
- this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.Button button1;
- private System.Windows.Forms.Button button2;
- private System.Windows.Forms.CheckBox checkBox1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement