public override void DrawRect(System.Drawing.RectangleF dirtyRect) { Graphics g = new Graphics(); int offset = 20; // Invert matrix: Matrix m = new Matrix(1, 2, 3, 4, 0, 0); g.DrawString("Original Matrix:", this.Font, Brushes.Black, 10, 10); DrawMatrix(m, g, 10, 10 + offset); g.DrawString("Inverted Matrix:", this.Font, Brushes.Black, 10, 10 + 2*offset); m.Invert(); DrawMatrix(m, g, 10, 10 + 3 * offset); // Matrix multiplication - MatrixOrder.Append: Matrix m1 = new Matrix(1, 2, 3, 4, 0, 1); Matrix m2 = new Matrix(0, 1, 2, 1, 0, 1); g.DrawString("Original Matrices:", this.Font, Brushes.Black, 10, 10 + 4 * offset); DrawMatrix(m1, g, 10, 10 + 5 * offset); DrawMatrix(m2, g, 10 + 130, 10 + 5 * offset); m1.Multiply(m2, MatrixOrder.Append); g.DrawString("Resultant Matrix - Append:", this.Font, Brushes.Black, 10, 10 + 6 * offset); DrawMatrix(m1, g, 10, 10 + 7 * offset); // Matrix multiplication - MatrixOrder.Prepend: m1 = new Matrix(1, 2, 3, 4, 0, 1); m1.Multiply(m2, MatrixOrder.Prepend); g.DrawString("Resultant Matrix - Prepend:", this.Font, Brushes.Black, 10, 10 + 8 * offset); DrawMatrix(m1, g, 10, 10 + 9 * offset); }
public SurfaceBackgroundChangeMemento(Surface surface, Matrix matrix) { _surface = surface; _image = surface.Image; _matrix = matrix.Clone(); // Make sure the reverse is applied _matrix.Invert(); }
/// <summary> /// use matrix to transform point /// </summary> /// <param name="pts">contain the points to be transform</param> private void TransFormPoints(Point[] pts) { System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix( 1, 0, 0, 1, this.openingPictureBox.Width / 2, this.openingPictureBox.Height / 2); matrix.Invert(); matrix.TransformPoints(pts); }
public override void Draw(CGRect rect) { Graphics g = Graphics.FromCurrentContext (); int offset = 20; // Invert matrix: var m = new Matrix (1, 2, 3, 4, 0, 0); g.DrawString ("Original Matrix:", Font, Brushes.Black, 10, 10); DrawMatrix (m, g, 10, 10 + offset); g.DrawString ("Inverted Matrix:", Font, Brushes.Black, 10, 10 + 2 * offset); m.Invert (); DrawMatrix (m, g, 10, 10 + 3 * offset); // Matrix multiplication - MatrixOrder.Append: var m1 = new Matrix (1, 2, 3, 4, 0, 1); var m2 = new Matrix (0, 1, 2, 1, 0, 1); g.DrawString ("Original Matrices:", Font, Brushes.Black, 10, 10 + 4 * offset); DrawMatrix (m1, g, 10, 10 + 5 * offset); DrawMatrix (m2, g, 10 + 130, 10 + 5 * offset); m1.Multiply (m2, MatrixOrder.Append); g.DrawString ("Resultant Matrix - Append:", Font, Brushes.Black, 10, 10 + 6 * offset); DrawMatrix (m1, g, 10, 10 + 7 * offset); // Matrix multiplication - MatrixOrder.Prepend: m1 = new Matrix (1, 2, 3, 4, 0, 1); m1.Multiply (m2, MatrixOrder.Prepend); g.DrawString ("Resultant Matrix - Prepend:", Font, Brushes.Black, 10, 10 + 8 * offset); DrawMatrix (m1, g, 10, 10 + 9 * offset); }
static public void TransformPoints(PointF[] points, PointF origin, bool diagonal, bool horizontal, bool vertical) { Matrix translate = new Matrix(); Matrix rotate = new Matrix(); // Put the points into origin/local space translate.Translate(-origin.X, -origin.Y); translate.TransformPoints(points); // Apply the flips/rotations (order matters) if (horizontal) { Matrix h = new Matrix(-1, 0, 0, 1, 0, 0); rotate.Multiply(h); } if (vertical) { Matrix v = new Matrix(1, 0, 0, -1, 0, 0); rotate.Multiply(v); } if (diagonal) { Matrix d = new Matrix(0, 1, 1, 0, 0, 0); rotate.Multiply(d); } // Apply the combined flip/rotate transformation rotate.TransformPoints(points); // Put points back into world space translate.Invert(); translate.TransformPoints(points); }
private static Matrix getAffineTransformMatrix(PointF p01, PointF p02, PointF p03, PointF p11, PointF p12, PointF p13) { Matrix a = new Matrix(p02.X - p01.X, p02.Y - p01.Y, p03.X - p01.X, p03.Y - p01.Y, p01.X, p01.Y); Matrix b = new Matrix(p12.X - p11.X, p12.Y - p11.Y, p13.X - p11.X, p13.Y - p11.Y, p11.X, p11.Y); if (!a.IsInvertible) return null; a.Invert(); a.Multiply(b, MatrixOrder.Append); return a; }
void generateTransformMat() { mTransformMat = new Matrix(); mTransformMat.Translate(mTranslation.X, mTranslation.Y); mTransformMat.Scale(mZoomAmt.Value, mZoomAmt.Value, MatrixOrder.Append); mTransformMatInv = mTransformMat.Clone(); mTransformMatInv.Invert(); }
/// <summary> /// Will return a copy of the Transform currently used to map the world Space of the draw Set to the pixel Space of the screen /// </summary> /// <returns></returns> public void setInverseCameraTransform() { inverseCameraTransform = cameraTransform.Clone(); inverseCameraTransform.Invert(); //adjusting for any warping as a result of differently sized buffers if (drawBuffers != null && (drawBuffers[0].Width != base.Width || drawBuffers[0].Height != base.Height)) { inverseCameraTransform.Scale(base.Width / drawBuffers[0].Width, base.Height / drawBuffers[0].Height, MatrixOrder.Append); } }
/// <summary> /// Sets the View parameters so that the <see cref="PlotModel"/>'s bounds world coordinates transform to device /// coordinates will fit in to the <see cref="Rectangle"/> bounds. /// </summary> public void SetBounds(PlotModel Model, Rectangle bounds) { ResetBounds(); RectangleF b = Bounds(Model); PointF[] v = new PointF[3]; v[0].X = bounds.X; v[0].Y = bounds.Y; v[1].X = bounds.X + bounds.Width; v[1].Y = bounds.Y; v[2].X = bounds.X; v[2].Y = bounds.Y + bounds.Height; T2D = new System.Drawing.Drawing2D.Matrix(b, v); T2Dinv = T2D.Clone(); T2Dinv.Invert(); }
private void SetupTransform() { //world points float x1 = 0, y1 = 0, x2 = 100, y2 = 100; //device points Rectangle crect = this.ClientRectangle; float x1d = (float)5 * crect.Width / 14;//up left corner float y1d = 0.1f * crect.Height;//up left corner float x2d = (float)13 * crect.Width / 14;//bottom rigth corner float y2d = 0.9f * crect.Height;//bottom right corner //calcutae the scalling s1 = (x1d - x2d) / (x1 - x2); s2 = (y1d - y2d) / (y1 - y2); t1 = (x1 * x2d - x2 * x1d) / (x1 - x2); t2 = (y1 * y2d - y2 * y1d) / (y1 - y2); m = new Matrix(); m.Translate(t1, t2);//transalation m.Scale(s1, s2);//scaling //get the inverse m.Invert(); minv = m.Clone(); m.Invert(); }
// Draw the graph. private void DrawGraph(Graphics gr) { // Map to turn right-side up and center at the origin. const float wxmin = -10; const float wymin = -10; const float wxmax = 10; const float wymax = 10; RectangleF rect = new RectangleF(wxmin, wymin, wxmax - wxmin, wymax - wymin); PointF[] pts = { new PointF(0, graphPictureBox.ClientSize.Height), new PointF(graphPictureBox.ClientSize.Width, graphPictureBox.ClientSize.Height), new PointF(0, 0), }; Matrix transform = new Matrix(rect, pts); gr.Transform = transform; // See how far it is between horizontal pixels in world coordinates. pts = new PointF[] { new PointF(1, 0) }; transform.Invert(); transform.TransformVectors(pts); float dx = pts[0].X; // Generate points on the curve. List<PointF> points = new List<PointF>(); for (float x = wxmin; x <= wxmax; x += dx) points.Add(new PointF(x, TheFunction(x))); // Use a thin pen. using (Pen thin_pen = new Pen(Color.Gray, 0)) { // Draw the coordinate axes. gr.DrawLine(thin_pen, wxmin, 0, wxmax, 0); gr.DrawLine(thin_pen, 0, wymin, 0, wymax); for (float x = wxmin; x <= wxmax; x++) gr.DrawLine(thin_pen, x, -0.5f, x, 0.5f); for (float y = wymin; y <= wymax; y++) gr.DrawLine(thin_pen, -0.5f, y, 0.5f, y); // Draw the graph. thin_pen.Color = Color.Red; //thin_pen.Color = Color.Black; gr.DrawLines(thin_pen, points.ToArray()); } }
Matrix2D trans_D2C; //显示拓扑坐标系(原点在minx,miny处)->窗口客户区坐标系 void UpdateMapping() //更新映射关系 { int offset_x = (ClientSize.Width - bmp.Width) / 2; int offset_y = (ClientSize.Height - bmp.Height) / 2; trans_D2C = new Matrix2D(1, 0, 0, -1, 0, display.maxy); //Y翻转 trans_D2C.Scale(1 / zoom, 1 / zoom, MatrixOrder.Append); //再缩放 trans_D2C.Translate(offset_x, offset_y, MatrixOrder.Append); //再平移 trans_D2C.Translate(delta.X, delta.Y, MatrixOrder.Append); //再平移(鼠标平移分量) //trans_C2D = new Matrix2D(); //trans_C2D.Translate(-delta.X, -delta.Y, MatrixOrder.Append); //先抵消鼠标平移分量 //trans_C2D.Translate(-offset_x, -offset_y, MatrixOrder.Append); //先平移回原点 //trans_C2D.Scale(zoom, zoom, MatrixOrder.Append); //反向缩放 //trans_C2D.Multiply(new Matrix2D(1, 0, 0, -1, 0, display.maxy), MatrixOrder.Append);//颠倒Y坐标轴 trans_C2D = trans_D2C.Clone(); trans_C2D.Invert(); }
// Calculate the world->client matrix and viewport that corresponds to the area we are viewing, // based on the size of the control and the centerPoint and zoom. The returned viewport has // Top and Bottom reversed, to correspond to the convention that Top <= Bottom. void CalculateWorldTransform() { // Get size, midpoint of the casvas in pixel coords. Size sizeInPixels = canvas.ClientSize; PointF midpoint = new PointF(sizeInPixels.Width / 2.0F, sizeInPixels.Height / 2.0F); // Calculate the world->canvas transform. Matrix matrix = new Matrix(); float scaleFactor = zoom; matrix.Translate(midpoint.X, midpoint.Y); matrix.Scale(scaleFactor, scaleFactor); matrix.Translate(-centerPoint.X, -centerPoint.Y); xformWorldToPixel = matrix.Clone(); // Invert it to get the window->world transform. matrix.Invert(); xformPixelToWorld = matrix; // Calculate the viewport in world coordinates. PointF[] pts = new PointF[] { new PointF(0.0F, 0.0F), new PointF((float) sizeInPixels.Width, (float) sizeInPixels.Height) }; xformPixelToWorld.TransformPoints(pts); viewport = new RectangleF(pts[0].X, pts[0].Y, pts[1].X - pts[0].X, pts[1].Y - pts[0].Y); }
protected MouseEventArgs BacktrackMouse(MouseEventArgs e) { float zoom = this.ZoomFactor; Matrix mx = new Matrix(zoom, 0, 0, zoom, 0, 0); mx.Translate(View.AutoScrollPosition.X * (1.0f / zoom), View.AutoScrollPosition.Y * (1.0f / zoom)); mx.Invert(); Point[] pa = new Point[] { new Point(e.X, e.Y) }; mx.TransformPoints(pa); return new MouseEventArgs(e.Button, e.Clicks, pa[0].X, pa[0].Y, e.Delta); }
/////////////////////////////////////////////////////////////////////////////// // Inherited methods // /////////////////////////////////////////////////////////////////////////////// #region OVERRIDES /// <summary> /// Overridden <see cref="VGElement.Draw(Graphics)"/>. /// Draws the arrow to the given graphics context. /// </summary> /// <param name="graphics">Graphics context to draw to</param> /// <exception cref="ArgumentNullException">Thrown, when graphics object is null.</exception> public override void Draw(Graphics graphics) { if (graphics == null) { throw new ArgumentNullException("Graphics object should not be null."); } int spacing = 5; // Drawarrow if (this.pointsAreSet) { float angle = (float)Math.Atan2(this.secondPoint.Y - this.firstPoint.Y, this.secondPoint.X - this.firstPoint.X); angle = (float)(angle * 180 / Math.PI); Matrix rotate = new Matrix(); rotate.RotateAt((float)(-angle), this.firstPoint); PointF[] pts = new PointF[] { this.firstPoint, this.secondPoint }; rotate.TransformPoints(pts); float width1 = this.firstPointWeight * this.scaleFactor; float width2 = this.secondPointWeight * this.scaleFactor; // Draw left and right arrow PointF tip1 = new PointF( pts[0].X + this.firstPointDistance, pts[0].Y); PointF topCorner1 = new PointF( pts[0].X + 1.5f * width1 + this.firstPointDistance, pts[0].Y + width1); PointF topLeftBar1 = new PointF( pts[0].X + 1.5f * width1 + this.firstPointDistance, pts[0].Y + 0.5f * width1); PointF topRightBar1 = new PointF( pts[1].X - 1.5f * width2 - spacing - this.secondPointDistance, pts[1].Y + 0.5f * width1); PointF bottomRightBar1 = new PointF( pts[1].X - 1.5f * width2 - spacing - this.secondPointDistance, pts[1].Y - 0.5f * width1); PointF bottomLeftBar1 = new PointF( pts[0].X + 1.5f * width1 + this.firstPointDistance, pts[0].Y - 0.5f * width1); PointF bottomCorner1 = new PointF( pts[0].X + 1.5f * width1 + this.firstPointDistance, pts[0].Y - width1); PointF tip2 = new PointF( pts[1].X - this.secondPointDistance, pts[1].Y); PointF bottomCorner2 = new PointF( pts[1].X - 1.5f * width2 - this.secondPointDistance, pts[1].Y - width2); PointF bottomRightBar2 = new PointF( pts[1].X - 1.5f * width2 - this.secondPointDistance, pts[1].Y - 0.5f * width2); PointF bottomLeftBar2 = new PointF( pts[0].X + 1.5f * width1 + spacing + this.firstPointDistance, pts[0].Y - 0.5f * width2); PointF topLeftBar2 = new PointF( pts[0].X + 1.5f * width1 + spacing + this.firstPointDistance, pts[0].Y + 0.5f * width2); PointF topRightBar2 = new PointF( pts[1].X - 1.5f * width2 - this.secondPointDistance, pts[1].Y + 0.5f * width2); PointF topCorner2 = new PointF( pts[1].X - 1.5f * width2 - this.secondPointDistance, pts[1].Y + width2); GraphicsPath firstArrow = new GraphicsPath(); GraphicsPath secondArrow = new GraphicsPath(); firstArrow.AddLines( new PointF[] { bottomRightBar1, bottomLeftBar1, bottomCorner1, tip1, topCorner1, topLeftBar1, topRightBar1 }); secondArrow.AddLines( new PointF[] { topLeftBar2, topRightBar2, topCorner2, tip2, bottomCorner2, bottomRightBar2, bottomLeftBar2 }); PointF[] firstPts = firstArrow.PathPoints; PointF[] secondPts = secondArrow.PathPoints; rotate.Invert(); rotate.TransformPoints(firstPts); rotate.TransformPoints(secondPts); firstArrow.Reset(); secondArrow.Reset(); firstArrow.AddLines(firstPts); secondArrow.AddLines(secondPts); if ((this.ShapeDrawAction & ShapeDrawAction.Fill) == ShapeDrawAction.Fill) { if (width1 > 0) { graphics.FillPath(this.Brush, firstArrow); } if (width2 > 0) { graphics.FillPath(this.Brush, secondArrow); } } if ((this.ShapeDrawAction & ShapeDrawAction.Edge) == ShapeDrawAction.Edge) { if (width1 > 0) { graphics.DrawPath(this.Pen, firstArrow); } if (width2 > 0) { graphics.DrawPath(this.Pen, secondArrow); } } if (!this.hideWeights) { string firstValue = this.firstPointWeight.ToString(this.formatString) + this.addOnString; string secondValue = this.secondPointWeight.ToString(this.formatString) + this.addOnString; SizeF sizeText1 = graphics.MeasureString(firstValue, this.weightFont); SizeF sizeText2 = graphics.MeasureString(secondValue, this.weightFont); PointF arrowBase1 = new PointF( pts[0].X + 1f * width1 + 30 + this.firstPointDistance, pts[0].Y); PointF arrowBase2 = new PointF( pts[1].X - 1f * width2 - 30 - this.secondPointDistance, pts[1].Y); PointF[] txtPoints = new PointF[] { arrowBase1, arrowBase2 }; rotate.TransformPoints(txtPoints); RectangleF textBounds1 = new RectangleF( txtPoints[0].X - sizeText1.Width / 2 - 1, txtPoints[0].Y - sizeText1.Height / 2 - 1, sizeText1.Width + 2, sizeText1.Height + 1); RectangleF textBounds2 = new RectangleF( txtPoints[1].X - sizeText2.Width / 2 - 1, txtPoints[1].Y - sizeText2.Height / 2 - 1, sizeText2.Width + 2, sizeText2.Height + 1); txtPoints[0].X -= sizeText1.Width / 2; txtPoints[1].X -= sizeText2.Width / 2; txtPoints[0].Y -= sizeText1.Height / 2; txtPoints[1].Y -= sizeText2.Height / 2; if (width1 > 0) { graphics.FillRectangle(new SolidBrush(Color.FromArgb(125, 255, 255, 255)), textBounds1); graphics.DrawString(firstValue, this.weightFont, new SolidBrush(this.weightFontColor), txtPoints[0]); } if (width2 > 0) { graphics.FillRectangle(new SolidBrush(Color.FromArgb(125, 255, 255, 255)), textBounds2); graphics.DrawString(secondValue, this.weightFont, new SolidBrush(this.weightFontColor), txtPoints[1]); } } } // Draw name and selection frame if applicable base.Draw(graphics); }
private void Translate(int x, int y) { SvgElementCollection.ISvgElementEnumerator enumerator1 = this.SVGDocument.SelectCollection.GetEnumerator(); while (enumerator1.MoveNext()) { SvgElement element1 = (SvgElement) enumerator1.Current; if (element1 == null) { return; } if (!(element1 is IGraph)) { return; } IGraph graph1 = (IGraph) element1; Matrix matrix3 =new Matrix(); if(graph1.LimitSize && graph1 is Use) { Use use1 =graph1 as Use; using (Matrix matrix1=this.picturePanel.CoordTransform.Clone()) { float f1 = 1/matrix1.Elements[0]; matrix3.Scale(f1,f1,MatrixOrder.Prepend); Symbol symbol = use1.RefElement as Symbol; RectangleF rf =symbol.GPath.GetBounds(symbol.Transform.Matrix); float f2 = (use1.X+rf.X+(rf.Width)/2)*(matrix1.Elements[0] -1); float f3 = (use1.Y+rf.Y+(rf.Height)/2)*(matrix1.Elements[0] -1); matrix3.Translate(f2,f3,MatrixOrder.Prepend); matrix3.Invert(); } } { Matrix matrix2 = graph1.Transform.Matrix.Clone(); matrix2.Multiply(matrix3); matrix2.Translate((float) x, (float) y, MatrixOrder.Append); graph1.Transform = new Transf(matrix2); } } }
protected void CalculateMatrix() { _cachedForwardMatrix.Reset(); _cachedForwardMatrix.Translate(_cachedLayerPosition.X, _cachedLayerPosition.Y); _cachedForwardMatrix.Scale((float)_location.Scale, (float)_location.Scale); _cachedForwardMatrix.Rotate(-(float)_location.Angle); _cachedReverseMatrix = _cachedForwardMatrix.Clone(); _cachedReverseMatrix.Invert(); }
/** You can use this matrix as follows: 1 - Create new object with identity matrix (empty constructor) 2 - Apply a set of transformations that you want to this matrix 3 - Transform the four corners of the original image to calculate 4 - The min X and min Y of the transformed image 5 - The width & height of the new image buffer 6 - Translate this matrix by (- min X, - min Y) 7 - Invert the matrix 8 - Use the inverted version to reverse transform all new locations in the new image buffer **/ public BufferedImage perform_concat_matrices_operations(BufferedImage _src_img, TextBox _console, float _shear_x = (float)0, float _shear_y = (float)0, float _scale_x = (float)1, float _scale_y = (float)1, float _rotate_theta = (float)0) { BufferedImage ret; // 1 - Create new object with identity matrix (empty constructor) Matrix transformations_matrix = new Matrix(); // 2 - Apply a set of transformations that you want to this matrix transformations_matrix.Rotate(_rotate_theta); transformations_matrix.Scale(_scale_x, _scale_y); transformations_matrix.Shear(_shear_x, _shear_y); // 3 - Transform the four corners of the original image to calculate /** * To get the size of the new (destination) image, * apply the forward mapping to the four corners of the original image * ([0,0], [W-1,0], [0,H-1], [W-1,H-1]) * to get their new locations. * Then use these new four locations to get the width and height of the destination image * (e.g. to get new width: * find min X & max X of the four new points and subtract them, * do the same for Y to get the new height) */ Point[] corner_points = { new Point(0, 0), new Point(_src_img.width - 1, 0), new Point(0, _src_img.height - 1), new Point(_src_img.width - 1, _src_img.height - 1) }; transformations_matrix.TransformPoints(corner_points); int min_x = find_min_x(corner_points); int min_y = find_min_y(corner_points); int max_x = find_max_x(corner_points); int max_y = find_max_y(corner_points); int new_width = max_x - min_x; int new_height = max_y - min_y; _console.Text += "Min X = "; _console.Text += min_x; _console.Text += Environment.NewLine; _console.Text += "Min Y = "; _console.Text += min_y; _console.Text += Environment.NewLine; _console.Text += "Max X = "; _console.Text += max_x; _console.Text += Environment.NewLine; _console.Text += "Max Y = "; _console.Text += max_y; _console.Text += Environment.NewLine; _console.Text += "New Width = "; _console.Text += new_width; _console.Text += Environment.NewLine; _console.Text += "New Height = "; _console.Text += new_height; _console.Text += Environment.NewLine; // 4 - The min X and min Y of the transformed image /** * To make the transformed image totally fit inside the buffer, a translation with (- min X, - min Y) should be appended to the original transformation matrix (W) before inverting it. */ // 6 - Translate this matrix by (- min X, - min Y) transformations_matrix.Translate(-min_x, -min_y); // 7 - Invert the matrix transformations_matrix.Invert(); // 8 - Use the inverted version to reverse transform all new locations in the new image buffer ret = apply_transformation_matrix_to_bitmap_or_buffer(transformations_matrix, _src_img, new_width, new_height, _console); return ret; }
public Size ClientSizeToLogical(Size clientSize) { //Scale the size, size scaling does not require translate Point[] points = new Point[] { new Point(clientSize) }; Matrix scalingMatrix = new Matrix(); scalingMatrix.Scale(ScaleZoomFactor, ScaleZoomFactor); scalingMatrix.Invert(); scalingMatrix.TransformPoints(points); scalingMatrix.Invert(); return new Size(points[0]); }
public void Transform(Matrix transform_matrix, Interpolation _interpolation = Interpolation.Bilinear) { PointF[] ps = new PointF[4]; ps[0].X = 0; ps[0].Y = 0; ps[1].X = width; ps[1].Y = 0; ps[2].X = 0; ps[2].Y = height; ps[3].X = width; ps[3].Y = height; transform_matrix.TransformPoints(ps); float MinX = ps[0].X, MinY = ps[0].Y, MaxX = ps[0].X, MaxY = ps[0].Y; for (int i = 0; i < 4; i++) { if (ps[i].X < MinX) MinX = ps[i].X; if (ps[i].Y < MinY) MinY = ps[i].Y; if (ps[i].X > MaxX) MaxX = ps[i].X; if (ps[i].Y > MaxY) MaxY = ps[i].Y; } int new_width = (int)(MaxX - MinX); int new_height = (int)(MaxY - MinY); transform_matrix.Translate(-MinX, -MinY, MatrixOrder.Append); if(transform_matrix.IsInvertible) { transform_matrix.Invert(); } bitmap = new Bitmap(new_width, new_height); Color[,] new_buffer2d = new Color[new_width, new_height]; PointF[] pt = new PointF[1]; for (int y = 0; y < new_height; y++) { for (int x = 0; x < new_width; x++) { pt[0].X = x; pt[0].Y = y; transform_matrix.TransformPoints(pt); if (pt[0].X < width - 1 && pt[0].Y < height - 1 && pt[0].X > 0 && pt[0].Y > 0) { Color color = Bilinear_Interpolate(pt[0]); switch (_interpolation) { case Interpolation.None: new_buffer2d[x, y] = buffer2d[(Int32)pt[0].X, (Int32)pt[0].Y]; break; case Interpolation.Bilinear: new_buffer2d[x, y] = color; break; default: new_buffer2d[x, y] = buffer2d[(Int32)pt[0].X, (Int32)pt[0].Y]; break; } } else { new_buffer2d[x, y] = Color.FromArgb(255, 255, 255); } bitmap.SetPixel(x, y, new_buffer2d[x, y]); } } width = new_width; height = new_height; buffer2d = new_buffer2d; }
public Size ClientSizeToLogical(Size clientSize) { Point[] pts = new Point[] { new Point(clientSize) }; Matrix matrix = new Matrix(); matrix.Scale(this.ScaleZoomFactor, this.ScaleZoomFactor); matrix.Invert(); matrix.TransformPoints(pts); matrix.Invert(); return new Size(pts[0]); }
/// <summary> /// Uses the current transform matrix to calculate the coordinates in terms of the unrotated, unflipped image /// </summary> /// <param name="location"></param> protected Point ClientToStandard(Point location) { Point result = location; Matrix flipRot = new Matrix(); Transform(flipRot); flipRot.Invert(); Point[] transformed = new[] { result }; flipRot.TransformPoints(transformed); return transformed[0]; }
/// <summary> /// Back Track the Mouse to return accurate coordinates regardless of zoom or pan effects. /// Courtesy of BobPowell.net <seealso cref="http://www.bobpowell.net/backtrack.htm"/> /// </summary> /// <param name="p">Point to backtrack</param> /// <returns>Backtracked point</returns> public Point BackTrackMouse(Point p) { // Backtrack the mouse... Point[] pts = new Point[] { p }; Matrix mx = new Matrix(); mx.Translate(-this.ClientSize.Width / 2, -this.ClientSize.Height / 2, MatrixOrder.Append); mx.Rotate(_rotation, MatrixOrder.Append); mx.Translate(this.ClientSize.Width / 2 + _panX, this.ClientSize.Height / 2 + _panY, MatrixOrder.Append); mx.Scale(_zoom, _zoom, MatrixOrder.Append); mx.Invert(); mx.TransformPoints(pts); return pts[0]; }
/// <summary> /// Back Track the Mouse to return accurate coordinates regardless of zoom or pan effects. /// </summary> /// <param name="p">Point to backtrack</param> /// <returns>Backtracked point</returns> public Point BackTrackMouse(Point p) { //if (isPainting) //{ Point[] pts = new Point[] { p }; Matrix mx = new Matrix(); mx.Translate(-ClientSize.Width / 2f, -ClientSize.Height / 2f, MatrixOrder.Append); mx.Rotate(_rotation, MatrixOrder.Append); mx.Translate(ClientSize.Width / 2f + _panX, ClientSize.Height / 2f + _panY, MatrixOrder.Append); mx.Scale(_zoom, _zoom, MatrixOrder.Append); mx.Invert(); mx.TransformPoints(pts); return pts[0]; //} //else //{ // Point[] pts = new Point[] { p }; // Matrix mx = new Matrix(); // mx.Rotate(_rotation, MatrixOrder.Append); // if (_zoom != lastzoom) // { // lastzoom = _zoom; // mx.Scale(_zoom, _zoom, MatrixOrder.Append); // mx.Translate(_panX, _panY, MatrixOrder.Append); // } // else // { // mx.Scale(_zoom, _zoom, MatrixOrder.Append); // mx.Translate(_panX, _panY, MatrixOrder.Append); // } // mx.Invert(); // mx.TransformPoints(pts); // return pts[0]; //} //return new Point(); }
// Computer the transform from coordinates of the bitmap to world coordinates Matrix BitmapTransform() { // (worldcoord in mm) / 25.4F * dpi = pixels float scaleFactor = bitmapDpi / 25.4F; Matrix matrix = new Matrix(); matrix.Translate(0, bitmap.PixelHeight); matrix.Scale(scaleFactor, -scaleFactor); matrix.Invert(); return matrix; }
/// <summary> /// Call once when app is launched to initialize for ink scaling with local system DPI settings /// </summary> /// <param name="x"></param> /// <param name="y"></param> public static void SetLocalDpi(float x, float y) { DpiX = x; DpiY = y; if ((DpiX != CANNONICAL_DPI) || (DpiY != CANNONICAL_DPI)) { NonStandardDpi = true; SendMatrix = new Matrix(DpiX / CANNONICAL_DPI, 0.0f, 0.0f, DpiY / CANNONICAL_DPI, 0.0f, 0.0f); ReceiveMatrix = SendMatrix.Clone(); ReceiveMatrix.Invert(); } }
/// <summary> /// Back Track the Mouse to return accurate coordinates regardless of zoom or pan effects. /// Courtesy of BobPowell.net <seealso cref="http://www.bobpowell.net/backtrack.htm"/> /// </summary> /// <param name="p">Point to backtrack</param> /// <returns>Backtracked point</returns> public Point BackTrackMouse(Point p) { if (isPainting) { Point[] pts = new Point[] { p }; Matrix mx = new Matrix(); mx.Translate(-ClientSize.Width / 2f, -ClientSize.Height / 2f, MatrixOrder.Append); mx.Rotate(_rotation, MatrixOrder.Append); mx.Translate(ClientSize.Width / 2f + _panX, ClientSize.Height / 2f + _panY, MatrixOrder.Append); mx.Scale(_zoom, _zoom, MatrixOrder.Append); mx.Invert(); mx.TransformPoints(pts); return pts[0]; } else { // Backtrack the mouse... Point[] pts = new Point[] { p }; Matrix mx = new Matrix(); mx.Rotate(_rotation, MatrixOrder.Append); // mx.Translate(ClientSize.Width / 2f + _panX, ClientSize.Height / 2f + _panY, MatrixOrder.Append); // mx.Rotate(_rotation, MatrixOrder.Append); if (_zoom != lastzoom) { //float X = ((FT_Status)this.ParentForm).wheelX; //float Y = ((FT_Status)this.ParentForm).wheelY; ////this.textBox5.Text = X.ToString(); //// this.textBox6.Text = Y.ToString(); //if (_zoom > lastzoom) //{ // _panX -= (int)(((_zoom - lastzoom) * (X - _panX)) / lastzoom); // _panY -= (int)(((_zoom - lastzoom) * (Y - _panY)) / lastzoom); //} //else //{ // _panX += (int)(((lastzoom - _zoom) * (X - _panX)) / lastzoom); // _panY += (int)(((lastzoom - _zoom) * (Y - _panY)) / lastzoom); //} lastzoom = _zoom; mx.Scale(_zoom, _zoom, MatrixOrder.Append); mx.Translate(_panX, _panY, MatrixOrder.Append); } else { mx.Scale(_zoom, _zoom, MatrixOrder.Append); mx.Translate(_panX, _panY, MatrixOrder.Append); } mx.Invert(); mx.TransformPoints(pts); return pts[0]; } }
/** You can use this matrix as follows: 1 - Create new object with identity matrix (empty constructor) 2 - Apply a set of transformations that you want to this matrix 3 - Transform the four corners of the original image to calculate 4 - The min X and min Y of the transformed image 5 - The width & height of the new image buffer 6 - Translate this matrix by (- min X, - min Y) 7 - Invert the matrix 8 - Use the inverted version to reverse transform all new locations in the new image buffer **/ public Bitmap perform_concat_matrices_operations(bufferedLockBitmap _src_img, float _shear_x = (float)0, float _shear_y = (float)0, float _scale_x = (float)1, float _scale_y = (float)1, float _rotate_theta = (float)0) { bufferedLockBitmap ret; // 1 - Create new object with identity matrix (empty constructor) Matrix transformations_matrix = new Matrix(); // 2 - Apply a set of transformations that you want to this matrix transformations_matrix.Rotate(_rotate_theta, MatrixOrder.Append); transformations_matrix.Scale(_scale_x, _scale_y, MatrixOrder.Append); transformations_matrix.Shear(_shear_x, _shear_y, MatrixOrder.Append); // 3 - Transform the four corners of the original image to calculate /** * To get the size of the new (destination) image, * apply the forward mapping to the four corners of the original image * ([0,0], [W-1,0], [0,H-1], [W-1,H-1]) * to get their new locations. * Then use these new four locations to get the width and height of the destination image * (e.g. to get new width: * find min X & max X of the four new points and subtract them, * do the same for Y to get the new height) */ Point[] corner_points = { new Point(0, 0), new Point(_src_img.source.Width - 1, 0), new Point(0, _src_img.source.Height - 1), new Point(_src_img.source.Width - 1, _src_img.source.Height - 1) }; transformations_matrix.TransformPoints(corner_points); int min_x = find_min_x(corner_points); int min_y = find_min_y(corner_points); int max_x = find_max_x(corner_points); int max_y = find_max_y(corner_points); int new_width =Math.Abs(Math.Abs(max_x) - Math.Abs(min_x));//edited int new_height = Math.Abs(max_y) - Math.Abs(min_y);//edited // 4 - The min X and min Y of the transformed image /** * To make the transformed image totally fit inside the buffer, a translation with (- min X, - min Y) should be appended to the original transformation matrix (W) before inverting it. */ // 6 - Translate this matrix by (- min X, - min Y) transformations_matrix.Translate(-min_x, -min_y, MatrixOrder.Append); // 7 - Invert the matrix transformations_matrix.Invert(); // 8 - Use the inverted version to reverse transform all new locations in the new image buffer return apply_transformation_matrix_to_bitmap_or_buffer(transformations_matrix, _src_img, new_width, new_height).source; }
private Point ClientPointToLogical(Point point, bool mapToLayout) { Point[] points = new Point[] { point }; //Translate the point Matrix translateMatrix = new Matrix(); translateMatrix.Translate(ScrollPosition.X, ScrollPosition.Y); translateMatrix.TransformPoints(points); //Scale down the point Matrix scalingMatrix = new Matrix(); scalingMatrix.Scale(ScaleZoomFactor, ScaleZoomFactor); scalingMatrix.Invert(); scalingMatrix.TransformPoints(points); scalingMatrix.Invert(); if (!mapToLayout) return points[0]; else return this.activeLayout.MapInCoOrdToLayout(points[0]); }
/// <summary> /// calculate the transform between canvas and geometry objects /// </summary> private void CalculateTransform() { PointF[] plgpts = CalculateCanvasRegion(); m_transform = new Matrix(BoundingBox, plgpts); m_inverse = m_transform.Clone(); if (m_inverse.IsInvertible) { m_inverse.Invert(); } }
/// <summary> /// Calculates all the parameters needed for this class to function. It assumes Center, ZoomLevel, ControlSize and Map as given. /// </summary> private void Calculate() { // Determine the size of the map first of all switch ( m_Map ) { case Maps.Felucca: case Maps.Trammel: // Issue 13 - ML map sizes - http://code.google.com/p/pandorasbox3/issues/detail?id=13 - Kons m_MapSize = MapSizes.FeluccaML; // Issue 13 - End break; case Maps.Ilshenar: // Issue 13 - ML map sizes - http://code.google.com/p/pandorasbox3/issues/detail?id=13 - Kons m_MapSize = MapSizes.Ilshenar; // Issue 13- End break; case Maps.Malas: // Issue 13 - ML map sizes - http://code.google.com/p/pandorasbox3/issues/detail?id=13 - Kons m_MapSize = MapSizes.Malas; // Issue 13 - End break; case Maps.Tokuno: m_MapSize = MapSizes.Tokuno; break; default: throw new Exception( string.Format( "Map not supported: {0}.", m_Map.ToString() ) ); } m_ControlSize = m_Viewer.Size; if ( m_RotateView ) { double d = Math.Sqrt( Math.Pow( m_ControlSize.Width + m_ControlSize.Height, 2 ) / 2 ); m_ImageSize = new Size( (int) Math.Round( d ), (int) Math.Round( d ) ); m_Transform = new Matrix( 1, 0, 0, 1, 0, 0 ); m_Transform.Translate( - ( m_ControlSize.Width - m_Viewer.Width ) / 2, - ( m_ControlSize.Height - m_Viewer.Height ) / 2 ); m_Transform.RotateAt( 45, new PointF( m_Viewer.Width / 2, m_Viewer.Height / 2 ), MatrixOrder.Append ); m_Transform.Invert(); } else { m_ImageSize = m_ControlSize; m_Transform = null; } // Calculate the number of cells displayed for each block m_CellsPerBlock = 8; if ( m_ZoomLevel < 0 ) { m_CellsPerBlock = 8 / ( (int) Math.Pow( 2, Math.Abs( m_ZoomLevel ) ) ); if ( m_CellsPerBlock < 1 ) m_CellsPerBlock = 1; } // Calculate the number of pixels used to display each block m_PixelsPerCell = 1; if ( m_ZoomLevel > 0 ) m_PixelsPerCell = (int) Math.Pow( 2, m_ZoomLevel ); // Calculate the number of cells in each pixel m_CellsPerPixel = 1.0 / m_PixelsPerCell; // Calculate the center of the control m_ControlCenter = new Point( m_Viewer.Width / 2, m_Viewer.Height / 2 ); // Calculate the BlockInfo Point bottomright = InternalControlToMap( new Point( m_ImageSize.Width - 1, m_ImageSize.Height - 1 ) ); m_End = new BlockInfo( bottomright, m_MapSize ); Point topleft = InternalControlToMap( new Point( 0, 0 ) ); m_Start = new BlockInfo( topleft, m_MapSize ); // Calculate the valid blocks BlockInfo start = m_Start; BlockInfo end = m_End; start.Validate(); end.Validate(); m_ValidXBlocks = end.XBlock - start.XBlock + 1; m_ValidYBlocks = end.YBlock - start.YBlock + 1; // Calculate the number of pixels needed to represent the left and top cells if ( m_PixelsPerCell > 1 ) { int yDiff = ( m_ImageSize.Height / 2 ) % m_PixelsPerCell; int xDiff = ( m_ImageSize.Width / 2 ) % m_PixelsPerCell; if ( yDiff == 0 ) m_TopPixels = m_PixelsPerCell; else m_TopPixels = yDiff; if ( xDiff == 0 ) m_LeftPixels = m_PixelsPerCell; else m_LeftPixels = xDiff; } else { m_TopPixels = 1; m_LeftPixels = 1; } // Calculate the bounds Point p1 = topleft; Point p2 = bottomright; m_Bounds = new Rectangle( p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y ); }
partial void InvertImpl() { t.Invert(); }
private Point ClientPointToLogical(Point point, bool mapToLayout) { Point[] pts = new Point[] { point }; Matrix matrix = new Matrix(); matrix.Translate((float) this.ScrollPosition.X, (float) this.ScrollPosition.Y); matrix.TransformPoints(pts); Matrix matrix2 = new Matrix(); matrix2.Scale(this.ScaleZoomFactor, this.ScaleZoomFactor); matrix2.Invert(); matrix2.TransformPoints(pts); matrix2.Invert(); if (!mapToLayout) { return pts[0]; } return this.activeLayout.MapInCoOrdToLayout(pts[0]); }
private void Plot_Paint(object sender, PaintEventArgs e) { if (series.Count == 0) { return; } Graphics G = e.Graphics; G.SmoothingMode = SmoothingMode.AntiAlias; G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; Pen axis = Pens.Black; Pen grid = Pens.LightGray; Font font = form.Font; // Compute plot area. RectangleF area = e.ClipRectangle; area.Inflate(-10.0f, -10.0f); area = PaintTitle(G, area); area = PaintAxisLabels(G, area); area = PaintLegend(G, area); // Draw background. G.FillRectangle(Brushes.White, area); G.DrawRectangle(Pens.Gray, area.Left, area.Top, area.Width, area.Height); // Compute plot bounds. PointF x0, x1; double y0 = _y0, y1 = _y1; if (double.IsNaN(y0) || double.IsNaN(y1)) { series.AutoBounds(_x0, _x1, out y0, out y1); } x0 = new PointF((float)_x0, (float)y0); x1 = new PointF((float)_x1, (float)y1); Matrix2D T = new Matrix2D( area, new PointF[] { new PointF(x0.X, x1.Y), new PointF(x1.X, x1.Y), new PointF(x0.X, x0.Y) }); T.Invert(); // Draw axes. double dx = Partition((x1.X - x0.X) / (Width / 80)); for (double x = x0.X - x0.X % dx; x <= x1.X; x += dx) { PointF tx = Tx(T, new PointF((float)x, 0.0f)); string s = x.ToString("G3"); SizeF sz = G.MeasureString(s, font); G.DrawString(s, font, Brushes.Black, new PointF(tx.X - sz.Width / 2, area.Bottom + 3.0f)); G.DrawLine(grid, new PointF(tx.X, area.Top), new PointF(tx.X, area.Bottom)); } double dy = Partition((x1.Y - x0.Y) / (Height / 50)); for (double y = x0.Y - x0.Y % dy; y <= x1.Y; y += dy) { PointF tx = Tx(T, new PointF(0.0f, (float)y)); string s = y.ToString("G3"); SizeF sz = G.MeasureString(s, font); G.DrawString(s, font, Brushes.Black, new PointF(area.Left - sz.Width, tx.Y - sz.Height / 2)); G.DrawLine(grid, new PointF(area.Left, tx.Y), new PointF(area.Right, tx.Y)); } G.DrawLine(axis, Tx(T, new PointF(x0.X, 0.0f)), Tx(T, new PointF(x1.X, 0.0f))); G.DrawLine(axis, Tx(T, new PointF(0.0f, x0.Y)), Tx(T, new PointF(0.0f, x1.Y))); G.DrawRectangle(Pens.Gray, area.Left, area.Top, area.Width, area.Height); G.SetClip(area); // Draw series. series.ForEach(i => i.Paint(T, x0.X, x1.X, G)); }
public Matrix GetInverseTransform() { if (mi != null) return mi; mi = GetTransform().Clone(); mi.Invert(); return mi; }
private Color[,] Transform(Color[,] Org_Buffer, Matrix m) { int width = Org_Buffer.GetLength(0); int height = Org_Buffer.GetLength(1); PointF[] p = new PointF[] { new PointF(0,0), new PointF(width,0), new PointF(0,height), new PointF(width,height)}; m.TransformPoints(p); float minx = 9999999, miny = 9999999; float maxx = 0, maxy = 0; getMinMax(p, out minx,out maxx,out miny,out maxy); float newWidth = maxx - minx; float newHeight = maxy - miny; if (flag_rotate_shear) m.Translate(-minx, -miny, MatrixOrder.Append); if (m.IsInvertible) m.Invert(); Color[,] c = new Color[(int)Math.Ceiling(newWidth), (int)Math.Ceiling(newHeight)]; PointF[] pnt = new PointF[1]; Bitmap res = new Bitmap((int)Math.Ceiling(newWidth), (int)Math.Ceiling(newHeight)); for (int i = 0; i < (int)newWidth; i++) { for (int j = 0; j < (int)newHeight; j++) { pnt[0].X = i; pnt[0].Y = j; m.TransformPoints(pnt); Data data; if (pnt[0].X < width && pnt[0].Y < height && pnt[0].X >= 0 && pnt[0].Y >= 0) { if (!flag_interpolate) { data.R_Val = Org_Buffer[(int)(pnt[0].X), (int)(pnt[0].Y)].R; data.G_Val = Org_Buffer[(int)(pnt[0].X), (int)(pnt[0].Y)].G; data.B_Val = Org_Buffer[(int)(pnt[0].X), (int)(pnt[0].Y)].B; c[i, j] = Color.FromArgb((int)data.R_Val, (int)data.G_Val, (int)data.B_Val); } else c[i, j] = interpolation(Org_Buffer, pnt[0]); } } } flag_rotate_shear = false; return c; }