0% found this document useful (0 votes)
24 views

2D Trasformacije: Zadatak 1: Kreirati Aplikaciju Prikazanu Na Slici

This document describes code for an application that allows 2D transformations on an image. The application contains radio buttons and text boxes to select and apply different transformations like translation, scaling, rotation, and shearing. The transformations are applied by modifying the transformation matrix and redrawing the image. Buttons allow resetting the transformation parameters and redrawing the image.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

2D Trasformacije: Zadatak 1: Kreirati Aplikaciju Prikazanu Na Slici

This document describes code for an application that allows 2D transformations on an image. The application contains radio buttons and text boxes to select and apply different transformations like translation, scaling, rotation, and shearing. The transformations are applied by modifying the transformation matrix and redrawing the image. Buttons allow resetting the transformation parameters and redrawing the image.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

2D Trasformacije

Zadatak 1: Kreirati aplikaciju prikazanu na slici

Potrebne kontrole

Panel: panel1

Radio Button: rbTranslation, rbScale, rbRotation, rbShear

TextBox kontrole: tbTranslationX, tbTranslationY, tbScaleX, tbScaleY, tbRotaionAngle, tbRotateAtX,


tbRotateAtY, tbShearX, tbShearY

Button: btnReset, btnShow

Labele: 6 komada

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Example2_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.BackColor = Color.White;
// Subscribing to a paint eventhandler to drawingPanel:
// Represents the method that will handle the Paint event of a Control
panel1.Paint += new PaintEventHandler(panel1Paint);
}

private void panel1Paint(object sender, PaintEventArgs e)


{
Graphics g = e.Graphics;
DrawAxes(g);
ApplyTransformation(g);
}

private void ApplyTransformation(Graphics g)


{
// Create a new transform matrix:
// Encapsulates a 3-by-3 affine matrix that represents a geometric transform.
This class cannot be inherited.
Matrix m = new Matrix();
// Bring origin to the center:
// Applies the specified translation vector to this Matrix by prepending the
translation vector
m.Translate(panel1.Width/2, panel1.Height/2);

if (rbTranslation.Checked)
{
// Translation:
int dx = Convert.ToInt16(tbTranslationX.Text);
int dy = - Convert.ToInt16(tbTranslationY.Text);
m.Translate(dx, dy);
}
else if (rbScale.Checked)
{
// Scaling:
float sx = Convert.ToSingle(tbScaleX.Text);
float sy = Convert.ToSingle(tbScaleY.Text);
m.Scale(sx, sy);
}
else if (rbRotation.Checked)
{
// Rotation:
float angle = Convert.ToSingle(tbRotaionAngle.Text);
float x = Convert.ToSingle(tbRotateAtX.Text);
float y = - Convert.ToSingle(tbRotateAtY.Text);
g.FillEllipse(Brushes.Black, x - 4, y - 4, 8, 8);
m.RotateAt(angle, new PointF(x, y));
}
else if (rbShear.Checked)
{
// Shear:
float alpha = Convert.ToSingle(tbShearX.Text);
float beta = Convert.ToSingle(tbShearY.Text);
m.Shear(alpha, beta);
}
g.Transform = m;
DrawHouse(g, Color.Black);
}

private void DrawHouse(Graphics g, Color color)


{
HatchBrush hb = new HatchBrush(HatchStyle.HorizontalBrick, color,
Color.White);
Pen aPen = new Pen(color, 2);
Point[] pta = new Point[5];
pta[0] = new Point(-40, 40);
pta[1] = new Point(40, 40);
pta[2] = new Point(40, -40);
pta[3] = new Point(0, -80);
pta[4] = new Point(-40, -40);
g.FillPolygon(hb, pta);
g.DrawPolygon(aPen, pta);
aPen.Dispose();
hb.Dispose();
}

private void DrawAxes(Graphics g)


{
Matrix m = new Matrix();
// Move the origin to center of panel1:
m.Translate(panel1.Width/2, panel1.Height/2);
// Apply the transformation
g.Transform = m;

// Draw x and y axes:


g.DrawLine(Pens.Blue, -panel1.Width / 2, 0, panel1.Width / 2, 0);
g.DrawLine(Pens.Blue, 0, -panel1.Height / 2, 0, panel1.Height / 2);
g.DrawString("X", this.Font, Brushes.Blue, panel1.Width / 2 - 20, -20);
g.DrawString("Y", this.Font, Brushes.Blue, 5, -panel1.Height / 2+5);

// Draw Ticks:
int tick = 40;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Far;
for (int i = -200; i <= 200; i += tick)
{
g.DrawLine(Pens.Blue, i, -3, i, 3);
g.DrawLine(Pens.Blue, -3, i, 3, i);

SizeF sizeXTick = g.MeasureString(i.ToString(), this.Font);


if (i != 0)
{
g.DrawString(i.ToString(), this.Font, Brushes.Blue,
i + sizeXTick.Width / 2, 4f, sf);
g.DrawString((-i).ToString(), this.Font, Brushes.Blue,
-3f, i - sizeXTick.Height / 2, sf);

}
else
{
g.DrawString("0", this.Font, Brushes.Blue,
new PointF(i - sizeXTick.Width / 3, 4f), sf);
}
}
}

private void btnReset_Click(object sender, EventArgs e)


{
// Reset parameters to default values:
tbTranslationX.Text = "0";
tbTranslationY.Text = "0";
tbScaleX.Text = "1";
tbScaleY.Text = "1";
tbRotaionAngle.Text = "0";
tbRotateAtX.Text = "0";
tbRotateAtY.Text = "0";
tbShearX.Text = "0";
tbShearY.Text = "0";
panel1.Invalidate();
}

private void btnShow_Click(object sender, EventArgs e)


{
// Invalidates a specific region of the control and causes a paint message to
be sent to the control
panel1.Invalidate();
}

}
}

You might also like