Re: Matrix - Transformation

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hello Peter,

ok thanks.
Here the code. I make two tests.

You need only a panel
this.panel_draw = new System.Windows.Forms.Panel();

Regards Thomas


First test.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Drawing.Drawing2D;

namespace Grafik
{
public partial class Form1 : Form
{
public List<CODES> ListCodes;

public class CODES
{
private double x;
private double y;

private int boardNumber;

public double X
{
get { return x; }
set { x = value; }
}

public double Y
{
get { return y; }
set { y = value; }
}

public int BoardNumber
{
get { return boardNumber; }
set { boardNumber = value; }
}

public void Init()
{
X = 0;
Y = 0;
boardNumber = 0;
}
}

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
ListCodes = new List<CODES>();
CODES obj;

int boardnumber = 0;
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 5; i++)
{
boardnumber++;
obj = new CODES();
obj.Init();
obj.X = obj.X + (j * 150);
obj.Y = obj.Y + (i * 70);
obj.BoardNumber = boardnumber;
ListCodes.Add(obj);
}
}
}

float ScaleX, ScaleY;

private void MapMillimetersToPixels(Graphics gfx, Rectangle
rectPixels, Rectangle rectMm)
{
Matrix matrix = new Matrix();
ScaleX = (float)rectPixels.Width / Math.Abs(rectMm.Width);
ScaleY = (float)rectPixels.Height /
Math.Abs(rectMm.Height);
matrix.Scale(ScaleX, ScaleY);
matrix.Translate(rectPixels.Left - ScaleX * rectMm.Left,
rectPixels.Top - ScaleY * rectMm.Top);

gfx.Transform = matrix;
}

private int MillimetersToPixels_X(double value)
{
return Convert.ToInt32(ScaleX * value);
}

private int MillimetersToPixels_Y(double value)
{
return Convert.ToInt32(ScaleY * value);
}

public void DrawPanel(List<CODES> listCodes, PaintEventArgs e)
{
Graphics gfx; // the Graphics instance; for example, taken from
PaintEventArgs
gfx = e.Graphics;


PointF ptCur;
string strPoint;

ptCur = new PointF();
foreach ( CODES code in listCodes )
{
ptCur.X = MillimetersToPixels_Y(code.X);
ptCur.Y = MillimetersToPixels_Y(code.Y);
ptCur.X = (float)code.X;
ptCur.Y = (float)code.Y;

// These two lines of code draw the cross
gfx.DrawLine( Pens.Black, ptCur.X - 5, ptCur.Y, ptCur.X +
5,ptCur.Y );
gfx.DrawLine( Pens.Black, ptCur.X, ptCur.Y - 5,
ptCur.X,ptCur.Y + 5 );

// This block of code draws the label for the point
using ( Brush brush = new SolidBrush( ForeColor ) )
{
strPoint = "P" + code.BoardNumber + "(" + code.X +","
+code.Y + ")";
gfx.DrawString( strPoint, Font, brush, ptCur.X +
10,ptCur.Y );
}
}


// calculation is finished -- now tranformation from mm to pixel


gfx.DrawRectangle(Pens.Red, 5, 5, 480, 380);


Rectangle rectMM; //= new Rectangle(0, 0, 900, 500);
Rectangle rectPixel = new Rectangle();
bool fZeroLeft, fZeroTop;
fZeroLeft = false;
fZeroTop = false;

int mmWidth, mmHeight;
mmWidth = 900;
mmHeight = 500;
rectMM = new Rectangle(
new Point(fZeroLeft ? 0 : -mmWidth,
fZeroTop ? 0 : -mmHeight),
new Size(fZeroLeft ? mmWidth : -mmWidth,
fZeroTop ? mmHeight : -mmHeight));

rectPixel = panel_draw.DisplayRectangle;

MapMillimetersToPixels( gfx, rectPixel, rectMM );


}

private void panel_draw_Paint(object sender, PaintEventArgs e)
{
DrawPanel(ListCodes, e);
}
}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Second test -- the font is here ok
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Panel
{
public partial class Form1 : Form
{
public List<Codes> listCodes;

/// <summary>Die Farbe, mit der die Buchstaben
/// neben dem Kreuz gezeichnet werden</summary>
Brush brush;

int anzX = 4;
int anzY = 5;
int abstandX = 50;
int abstandY = 30;

/// <summary>Radius des Kreuzes</summary>
int kRad = 5;

/// <summary>X-Abstand der Buchstaben vom Punkt</summary>
int xPunktAbstand = 10;

/// <summary>Skalierung der gesamten Grafik. 1=nicht
skaliert.</summary>
float gesamtSkalierung = 1;

float fontGrössePunktBeschreibung = 6.0F;
Font punktBeschreibungsFont;

public class Codes
{
private double x;
private double y;
private int boardNumber;

public double X
{
get { return x; }
set { x = value; }
}

public double Y
{
get { return y; }
set { y = value; }
}

public PointF GetPointF()
{
return new PointF((float)x, (float)y);
}

public int BoardNumber
{
get { return boardNumber; }
set { boardNumber = value; }
}

public Codes(double x, double y, int boardNumber)
{
this.x = x;
this.y = y;
this.boardNumber = boardNumber;
}
}

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
ListCodesFüllen(ref listCodes, anzX, anzY, abstandX,
abstandY);
//brush = new SolidBrush(this.ForeColor);
brush = new SolidBrush(Color.Red);

punktBeschreibungsFont = new Font("Arial",
fontGrössePunktBeschreibung);
}

private void ListCodesFüllen(ref List<Codes> listCodes, int
anzX, int anzY, int abstandX, int abstandY)
{
listCodes = new List<Codes>();
Codes codes; int boardNumber = 0;

for (int x = 0; x < anzX; x++)
{
for (int y = 0; y < anzY; y++)
{
boardNumber++;
codes = new Codes(x * abstandX, y * abstandY,
boardNumber);
listCodes.Add(codes);
}
}
}

public void DrawPanel(List<Codes> listCodes, PaintEventArgs e)
{
Graphics g = e.Graphics;
PointF ptCur;
string strPoint;
// momentan keine Auswirkung
g.ScaleTransform(gesamtSkalierung, gesamtSkalierung);

ptCur = new PointF();
foreach (Codes code in listCodes)
{
ptCur = MillimeterToPixelPointF(g, code.GetPointF());

// Zeichnen der zwei Linien, die das Kreuz bilden
g.DrawLine(Pens.Black, ptCur.X - kRad, ptCur.Y,
ptCur.X + kRad, ptCur.Y);
g.DrawLine(Pens.Black, ptCur.X, ptCur.Y - kRad,
ptCur.X, ptCur.Y + kRad);

// Buchstaben und Bezeichnungen für den Punkt zeichnen
strPoint = "P" + code.BoardNumber + "(" + code.X + ","
+ code.Y + ")";
//g.DrawString(strPoint, Font, brush, ptCur.X +
xPunktAbstand, ptCur.Y);
g.DrawString(strPoint, punktBeschreibungsFont, brush,
ptCur.X + xPunktAbstand, ptCur.Y);
}

ZeichneAuswahlRechteck(g, 1, 1, 2, 2);
}

private void ZeichneAuswahlRechteck(Graphics g, int links, int
rechts, int breite, int höhe)
{
Point p1 = Point.Ceiling(MillimeterToPixelPointF(
g, new PointF(links * abstandX, rechts * abstandY)));
Point p2 = Point.Ceiling(MillimeterToPixelPointF(
g, new PointF(breite * abstandX, höhe * abstandY)));
Rectangle r = new Rectangle(p1.X, p1.Y, p2.X, p2.Y);
g.DrawRectangle(Pens.Red, r);
}

float zollAlsMillimeter = 25.4F;

PointF MillimeterToPixelPointF(Graphics g, PointF p)
{
p.X *= g.DpiX / zollAlsMillimeter;
p.Y *= g.DpiY / zollAlsMillimeter;
return p;
}

private void panel_draw_Paint(object sender, PaintEventArgs e)
{
DrawPanel(listCodes, e);
}
}

}


.



Relevant Pages

  • Re: C#- coordinate system, drawing
    ... public class CODES ... private double x; ... public int BoardNumber ... private void MapMillimetersToPixels(Graphics gfx, Rectangle ...
    (microsoft.public.dotnet.languages.csharp)
  • poker odds
    ... public static final int HIGH_CARD = 0; ... private static Logger logger; ... public Poker(int numCard, Card deck) ... public void maxSimulation ...
    (comp.lang.java.programmer)
  • problem with vmr9 image compositor - method Draw
    ... I play video with playlist, if i play video about 5x it is ... but so 6round of video playing throw exception int ... private void ConfigureVMR9InWindowlessMode() ... Surface renderTarget = new Surface; ...
    (microsoft.public.win32.programmer.directx.managed)
  • Re: using ref keyword performance
    ... // private fields... ... int valueCount = values.Length; ... static void TestWithRefLoop() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: The code for "sorting in C sharp considered superior" in five parts for easier download
    ... private bool interchangeSortTest() ... ("Sequence error after interchange sort", ... private bool labelHasFontTag ... int intGridTop, ...
    (comp.programming)