我的任务是:用几条线构建一个图形并对其进行仿射变换。我也需要自己做这个转换:计算新坐标并用矩阵转换移动图形。但是当我尝试调用我的方法 DrawLine 时,我有一个错误“无效参数”。你能帮助我吗?
矩阵类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Smile
{
class Matrix
{
double[,] matrix;
public Matrix(double[,] matrix)
{
this.matrix = matrix;
}
public int cols
{
get
{
return this.matrix.GetUpperBound(1) + 1;
}
}
public int rows
{
get
{
return this.matrix.Length / this.cols;
}
}
public double getElement(int row, int col)
{
return this.matrix[row, col];
}
}
}
箭头类(这是一个图,我从线条构建的)我对 DrawArrow() 有问题,然后尝试将我的箭头指向新坐标。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace Smile
{
class Arrow
{
Pen p = new Pen(Color.Blue, 5);// цвет линии и ширина
//Graphics graphics;
Matrix affine, dots;
public Arrow(Graphics graphics)
{
//this.graphics = graphics;
this.DrawArrow(5, 10, 40, 100, 40, 60, 10, 70, graphics);
}
private void DrawArrow(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, Graphics graphics)
{
graphics.DrawLine(this.p, new Point(x1, y1), new Point(x2, y2));
graphics.DrawLine(this.p, new Point(x2, y2), new Point(x3, y3));
graphics.DrawLine(this.p, new Point(x2, y2), new Point(x4, y4));
initializeDotMatrix(x1, y1, x2, y2, x3, y3, x4, y4);
}
public void createAffine(double a, double b, double c, double d)
{
double[,] affine = { { a, b }, { c, d } };
this.affine = new Matrix(affine);
}
private void initializeDotMatrix(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
double[,] dots = { { x1, y1 }, { x2, y2 }, { x3, y3 }, { x4, y4 } };
this.dots = new Matrix(dots);
}
private void reInitializeLines(int[,] coordinates, Graphics graphics)
{
DrawArrow(coordinates[0, 0],
coordinates[0, 1],
coordinates[1, 0],
coordinates[1, 1],
coordinates[2, 0],
coordinates[2, 1],
coordinates[3, 0],
coordinates[3, 1], graphics);
}
public void moveRight(int step, Graphics graphics)
{
createAffine(1, step, 0, 1);
reInitializeLines(this.multiply(), graphics);
}
public int[,] multiply()
{
int[,] result = new int[dots.rows, affine.cols];
for (int resultRow = 0; resultRow < dots.rows; resultRow++)
{
for (int resultCol = 0; resultCol < affine.cols; resultCol++)
{
double value = 0;
for (int i = 0; i < affine.cols; i++)
{
value += dots.getElement(resultRow, i) * affine.getElement(i, resultCol);
}
result[resultRow, resultCol] = Convert.ToInt32(value);
}
}
return result;
}
}
}
表单类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Smile
{
public partial class Form1 : Form
{
Arrow arrow;
Graphics gr;
bool changed = true;
bool isExist = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
this.gr = e.Graphics;
arrow = new Arrow(gr);
}
protected override void OnPaint(PaintEventArgs e)
{
if (!isExist)
{
isExist = false;
this.gr = e.Graphics;
arrow = new Arrow(gr);
}
/*if (changed)
{
changed = false;
//gr.Invalidate();
gr.Clear(Color.White);
base.OnPaint(e);
}*/
}
private void Right_Click(object sender, EventArgs e)
{
changed = true;
arrow.moveRight(200, gr);
}
}
}