Benutzer mit den meisten Antworten
Koordinatensystem

Frage
-
Hey,
ich will ein Koordinaten System in C# erstellen, weis aber nicht wie ich das anstellen soll, kann mir jemand diskret sagen womit ich da anfangen soll, denn im Internet habe ich überhaupt nichts gutes dazu gefunden.
Ich muss noch sagen, dass ich Anfänger in C# bin, aber trotzdem sehr gut in Java sowie Webentwicklung bin.
Danke im Voraus.
LG Steven
Antworten
-
Hi,
naja, vielleicht so: (hier mit MouseClick Punkte hinzufügen...)
public partial class Form1 : Form { private MyCoordPanel p1 = new MyCoordPanel(); public Form1() { InitializeComponent(); p1.Dock = DockStyle.Fill; p1.ForeColor = Color.Red; this.Controls.Add(p1); //clicken, um Punkte hinzuzufügen... this.p1.MouseClick += P1_MouseClick; } private void P1_MouseClick(object sender, MouseEventArgs e) { p1.Data.Add(new PointF(e.X - p1.ClientSize.Width / 2, e.Y - p1.ClientSize.Height / 2)); p1.Invalidate(); } } public class MyCoordPanel : Panel { public List<PointF> Data { get; set; } public float Abstand { get; set; } public MyCoordPanel() { this.DoubleBuffered = true; this.BackColor = SystemColors.ControlDark; this.Abstand = 10; this.Data = new List<PointF>(); } protected override void OnResize(EventArgs eventargs) { base.OnResize(eventargs); this.Invalidate(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); PointF cntr = new PointF(this.ClientSize.Width / 2, this.ClientSize.Height / 2); int xx = (int)(cntr.X / Abstand) * (int)Abstand; int yy = (int)(cntr.Y / Abstand) * (int)Abstand; e.Graphics.TranslateTransform(cntr.X, cntr.Y); for (float x = -xx; x < xx; x += Abstand) { e.Graphics.DrawLine(Pens.Black, x, -cntr.Y, x, cntr.Y); } for (float y = -yy; y < yy; y += Abstand) { e.Graphics.DrawLine(Pens.Black, -cntr.X, y, cntr.X, y); } e.Graphics.DrawLine(Pens.Blue, 0, -cntr.Y, 0, cntr.Y); e.Graphics.DrawLine(Pens.Blue, -cntr.X, 0, cntr.X, 0); //drawLegend mit e.Graphics.DrawString(...); if (this.Data != null && this.Data.Count > 0) { if (Data.Count == 1) using (Pen pen = new Pen(this.ForeColor, 1)) e.Graphics.DrawEllipse(pen, new RectangleF(this.Data[0].X, this.Data[0].Y, 1, 1)); else using (System.Drawing.Drawing2D.GraphicsPath gP = new System.Drawing.Drawing2D.GraphicsPath()) { gP.AddLines(this.Data.ToArray()); e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; using (Pen pen = new Pen(this.ForeColor, 1)) e.Graphics.DrawPath(pen, gP); } } } }
Viele Grüße,
Thorsten
- Bearbeitet Thorsten Gudera Freitag, 20. April 2018 08:12
- Als Antwort vorgeschlagen Ivan DragovMicrosoft contingent staff, Moderator Montag, 23. April 2018 05:23
- Als Antwort markiert Ivan DragovMicrosoft contingent staff, Moderator Montag, 30. April 2018 09:25
-
Hi,
nimm ein control auf dem das Koordinatensystem gezeichnet werden soll, zB. ein Panel, handle, oder überschreibe die Paint methode und iteriere in dieser Methode mit gewähltem Koordinatenabstand über die Oberfläche in x und y Richtung und zeichne Linien.
public partial class Form1 : Form { private MyCoordPanel p1 = new MyCoordPanel(); public Form1() { InitializeComponent(); p1.Dock = DockStyle.Fill; this.Controls.Add(p1); } } public class MyCoordPanel : Panel { public float Abstand { get; set; } public MyCoordPanel() { this.DoubleBuffered = true; this.BackColor = SystemColors.ControlDark; this.Abstand = 10; } protected override void OnResize(EventArgs eventargs) { base.OnResize(eventargs); this.Invalidate(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); PointF cntr = new PointF(this.ClientSize.Width / 2, this.ClientSize.Height / 2); int xx = (int)(cntr.X / Abstand) * (int)Abstand; int yy = (int)(cntr.Y / Abstand) * (int)Abstand; e.Graphics.TranslateTransform(cntr.X, cntr.Y); //setze Ursprung in die Mitte for (float x = -xx; x < xx; x += Abstand) { e.Graphics.DrawLine(Pens.Black, x, -cntr.Y, x, cntr.Y); } for (float y = -yy; y < yy; y += Abstand) { e.Graphics.DrawLine(Pens.Black, -cntr.X, y, cntr.X, y); } e.Graphics.DrawLine(Pens.Blue, 0, -cntr.Y, 0, cntr.Y); e.Graphics.DrawLine(Pens.Blue, -cntr.X, 0, cntr.X, 0); //drawLegend mit e.Graphics.DrawString(...); } }
Viele Grüße,
Thorsten
- Bearbeitet Thorsten Gudera Donnerstag, 19. April 2018 18:31
- Als Antwort vorgeschlagen Ivan DragovMicrosoft contingent staff, Moderator Montag, 23. April 2018 05:23
- Als Antwort markiert Ivan DragovMicrosoft contingent staff, Moderator Montag, 30. April 2018 09:25
Alle Antworten
-
Ich habe keine Idee was Du mit "ein Koodinaten System in C# erstellen" meinst.
Eine Koordinate ist ein Punkt auf einer Fläche, dessen Position durch seine Postion auf der Abzisse und Ordinate angegeben wird - bei einem Punkt im Raum kommt noch die Applikate (oder auch Kote) hinzu.
Du kannst eine Koordinate mit einem Paar (oder Triple im Raum) im Speicher halten, z. B. mit der Klasse Tupel (2-Tupel oder 3-Tupel).
- Gruß Florian
-
Hi,
nimm ein control auf dem das Koordinatensystem gezeichnet werden soll, zB. ein Panel, handle, oder überschreibe die Paint methode und iteriere in dieser Methode mit gewähltem Koordinatenabstand über die Oberfläche in x und y Richtung und zeichne Linien.
public partial class Form1 : Form { private MyCoordPanel p1 = new MyCoordPanel(); public Form1() { InitializeComponent(); p1.Dock = DockStyle.Fill; this.Controls.Add(p1); } } public class MyCoordPanel : Panel { public float Abstand { get; set; } public MyCoordPanel() { this.DoubleBuffered = true; this.BackColor = SystemColors.ControlDark; this.Abstand = 10; } protected override void OnResize(EventArgs eventargs) { base.OnResize(eventargs); this.Invalidate(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); PointF cntr = new PointF(this.ClientSize.Width / 2, this.ClientSize.Height / 2); int xx = (int)(cntr.X / Abstand) * (int)Abstand; int yy = (int)(cntr.Y / Abstand) * (int)Abstand; e.Graphics.TranslateTransform(cntr.X, cntr.Y); //setze Ursprung in die Mitte for (float x = -xx; x < xx; x += Abstand) { e.Graphics.DrawLine(Pens.Black, x, -cntr.Y, x, cntr.Y); } for (float y = -yy; y < yy; y += Abstand) { e.Graphics.DrawLine(Pens.Black, -cntr.X, y, cntr.X, y); } e.Graphics.DrawLine(Pens.Blue, 0, -cntr.Y, 0, cntr.Y); e.Graphics.DrawLine(Pens.Blue, -cntr.X, 0, cntr.X, 0); //drawLegend mit e.Graphics.DrawString(...); } }
Viele Grüße,
Thorsten
- Bearbeitet Thorsten Gudera Donnerstag, 19. April 2018 18:31
- Als Antwort vorgeschlagen Ivan DragovMicrosoft contingent staff, Moderator Montag, 23. April 2018 05:23
- Als Antwort markiert Ivan DragovMicrosoft contingent staff, Moderator Montag, 30. April 2018 09:25
-
Hi,
nimm ein control auf dem das Koordinatensystem gezeichnet werden soll, zB. ein Panel, handle, oder überschreibe die Paint methode und iteriere in dieser Methode mit gewähltem Koordinatenabstand über die Oberfläche in x und y Richtung und zeichne Linien.
public partial class Form1 : Form { private MyCoordPanel p1 = new MyCoordPanel(); public Form1() { InitializeComponent(); p1.Dock = DockStyle.Fill; this.Controls.Add(p1); } } public class MyCoordPanel : Panel { public float Abstand { get; set; } public MyCoordPanel() { this.DoubleBuffered = true; this.BackColor = SystemColors.ControlDark; this.Abstand = 10; } protected override void OnResize(EventArgs eventargs) { base.OnResize(eventargs); this.Invalidate(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); PointF cntr = new PointF(this.ClientSize.Width / 2, this.ClientSize.Height / 2); int xx = (int)(cntr.X / Abstand) * (int)Abstand; int yy = (int)(cntr.Y / Abstand) * (int)Abstand; e.Graphics.TranslateTransform(cntr.X, cntr.Y); //setze Ursprung in die Mitte for (float x = -xx; x < xx; x += Abstand) { e.Graphics.DrawLine(Pens.Black, x, -cntr.Y, x, cntr.Y); } for (float y = -yy; y < yy; y += Abstand) { e.Graphics.DrawLine(Pens.Black, -cntr.X, y, cntr.X, y); } e.Graphics.DrawLine(Pens.Blue, 0, -cntr.Y, 0, cntr.Y); e.Graphics.DrawLine(Pens.Blue, -cntr.X, 0, cntr.X, 0); //drawLegend mit e.Graphics.DrawString(...); } }
Viele Grüße,
Thorsten
Vielen Dank das hat sehr geholfen nur wie tue ich jetzt den x und y Punkt einzeichnen spricht ich übergebe aus einem anderen Form ein X und Y Wert und diese sollen im Koordinatensystem angezeigt werden und miteinander verbunden werden?
LG Steven
-
Hi,
naja, vielleicht so: (hier mit MouseClick Punkte hinzufügen...)
public partial class Form1 : Form { private MyCoordPanel p1 = new MyCoordPanel(); public Form1() { InitializeComponent(); p1.Dock = DockStyle.Fill; p1.ForeColor = Color.Red; this.Controls.Add(p1); //clicken, um Punkte hinzuzufügen... this.p1.MouseClick += P1_MouseClick; } private void P1_MouseClick(object sender, MouseEventArgs e) { p1.Data.Add(new PointF(e.X - p1.ClientSize.Width / 2, e.Y - p1.ClientSize.Height / 2)); p1.Invalidate(); } } public class MyCoordPanel : Panel { public List<PointF> Data { get; set; } public float Abstand { get; set; } public MyCoordPanel() { this.DoubleBuffered = true; this.BackColor = SystemColors.ControlDark; this.Abstand = 10; this.Data = new List<PointF>(); } protected override void OnResize(EventArgs eventargs) { base.OnResize(eventargs); this.Invalidate(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); PointF cntr = new PointF(this.ClientSize.Width / 2, this.ClientSize.Height / 2); int xx = (int)(cntr.X / Abstand) * (int)Abstand; int yy = (int)(cntr.Y / Abstand) * (int)Abstand; e.Graphics.TranslateTransform(cntr.X, cntr.Y); for (float x = -xx; x < xx; x += Abstand) { e.Graphics.DrawLine(Pens.Black, x, -cntr.Y, x, cntr.Y); } for (float y = -yy; y < yy; y += Abstand) { e.Graphics.DrawLine(Pens.Black, -cntr.X, y, cntr.X, y); } e.Graphics.DrawLine(Pens.Blue, 0, -cntr.Y, 0, cntr.Y); e.Graphics.DrawLine(Pens.Blue, -cntr.X, 0, cntr.X, 0); //drawLegend mit e.Graphics.DrawString(...); if (this.Data != null && this.Data.Count > 0) { if (Data.Count == 1) using (Pen pen = new Pen(this.ForeColor, 1)) e.Graphics.DrawEllipse(pen, new RectangleF(this.Data[0].X, this.Data[0].Y, 1, 1)); else using (System.Drawing.Drawing2D.GraphicsPath gP = new System.Drawing.Drawing2D.GraphicsPath()) { gP.AddLines(this.Data.ToArray()); e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; using (Pen pen = new Pen(this.ForeColor, 1)) e.Graphics.DrawPath(pen, gP); } } } }
Viele Grüße,
Thorsten
- Bearbeitet Thorsten Gudera Freitag, 20. April 2018 08:12
- Als Antwort vorgeschlagen Ivan DragovMicrosoft contingent staff, Moderator Montag, 23. April 2018 05:23
- Als Antwort markiert Ivan DragovMicrosoft contingent staff, Moderator Montag, 30. April 2018 09:25
-
Hi,
naja, vielleicht so: (hier mit MouseClick Punkte hinzufügen...)
public partial class Form1 : Form { private MyCoordPanel p1 = new MyCoordPanel(); public Form1() { InitializeComponent(); p1.Dock = DockStyle.Fill; p1.ForeColor = Color.Red; this.Controls.Add(p1); //clicken, um Punkte hinzuzufügen... this.p1.MouseClick += P1_MouseClick; } private void P1_MouseClick(object sender, MouseEventArgs e) { p1.Data.Add(new PointF(e.X - p1.ClientSize.Width / 2, e.Y - p1.ClientSize.Height / 2)); p1.Invalidate(); } } public class MyCoordPanel : Panel { public List<PointF> Data { get; set; } public float Abstand { get; set; } public MyCoordPanel() { this.DoubleBuffered = true; this.BackColor = SystemColors.ControlDark; this.Abstand = 10; this.Data = new List<PointF>(); } protected override void OnResize(EventArgs eventargs) { base.OnResize(eventargs); this.Invalidate(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); PointF cntr = new PointF(this.ClientSize.Width / 2, this.ClientSize.Height / 2); int xx = (int)(cntr.X / Abstand) * (int)Abstand; int yy = (int)(cntr.Y / Abstand) * (int)Abstand; e.Graphics.TranslateTransform(cntr.X, cntr.Y); for (float x = -xx; x < xx; x += Abstand) { e.Graphics.DrawLine(Pens.Black, x, -cntr.Y, x, cntr.Y); } for (float y = -yy; y < yy; y += Abstand) { e.Graphics.DrawLine(Pens.Black, -cntr.X, y, cntr.X, y); } e.Graphics.DrawLine(Pens.Blue, 0, -cntr.Y, 0, cntr.Y); e.Graphics.DrawLine(Pens.Blue, -cntr.X, 0, cntr.X, 0); //drawLegend mit e.Graphics.DrawString(...); if (this.Data != null && this.Data.Count > 0) { if (Data.Count == 1) using (Pen pen = new Pen(this.ForeColor, 1)) e.Graphics.DrawEllipse(pen, new RectangleF(this.Data[0].X, this.Data[0].Y, 1, 1)); else using (System.Drawing.Drawing2D.GraphicsPath gP = new System.Drawing.Drawing2D.GraphicsPath()) { gP.AddLines(this.Data.ToArray()); e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; using (Pen pen = new Pen(this.ForeColor, 1)) e.Graphics.DrawPath(pen, gP); } } } }
Viele Grüße,
Thorsten
Ok vielen viel Dank du hast mir sehr geholfen.
LG Steven