I just want to transform the points when scrolling. I already use the e.Graphics.TranslateTransform() but it only works on drawing. The point of the line wont change the offset.
private void Form1_Scroll(object sender, ScrollEventArgs e)
{
Point scrollOffset = this.AutoScrollPosition;
for (int lk = 0; lk < Pt1.Count; lk++)
{
Pt1[lk] = new Point(Pt1[lk].X + scrollOffset.X, Pt1[lk].Y + scrollOffset.Y);
Pt2[lk] = new Point(Pt2[lk].X + scrollOffset.X, Pt2[lk].Y + scrollOffset.Y);
}
this.Invalidate();
this.Update();
this.Refresh();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Point scrollOffset = this.AutoScrollPosition;
e.Graphics.TranslateTransform(scrollOffset.X, scrollOffset.Y);
e.Graphics.Clear(this.BackColor);
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Pen greenPen = new Pen(Color.Green,1);
greenPen.DashPattern = new float[] { 5.0F, 5.0F, 5.0F, 5.0F };
Pen red = new Pen(Color.Red,1);
red.DashPattern = new float[] { 5.0F, 5.0F, 5.0F, 5.0F };
Pen Grey = new Pen(Color.Gray,1);
Grey.DashPattern = new float[] { 5.0F, 5.0F, 5.0F, 5.0F };
#region "Draw Line using cursor"
Pen SolidGreen = new Pen(Color.Green, 1);
Pen SolidGrey = new Pen(Color.Gray, 1);
Pen Red = new Pen(Color.Red, 1);
for (int pl = 0; pl < Pt1j.Count; pl++)
{
PointF[] pointsj = new PointF[]
{
new PointF(Pt2j[pl].X, Pt2j[pl].Y),
new PointF(Pt3j[pl].X , Pt3j[pl].Y),
new PointF(Pt4j[pl].X , Pt4j[pl].Y),
new PointF(Pt5j[pl].X , Pt5j[pl].Y),
};
e.Graphics.DrawLine(Pens.Green, Pt1j[pl].X , Pt1j[pl].Y, Pt2j[pl].X, Pt2j[pl].Y);
e.Graphics.DrawCurve(Pens.Green, pointsj);
e.Graphics.DrawLine(Pens.Green, Pt5j[pl].X , Pt5j[pl].Y, Pt6j[pl].X, Pt6j[pl].Y);
}
for (int pl = 0; pl < Pt1DashedGray.Count; pl++)
{
e.Graphics.DrawLine(Grey, Pt1DashedGray[pl], Pt2DashedGray[pl]);
PointF[] pointsdhg = new PointF[]
{
new PointF(Pt2DashedGray[pl].X, Pt2DashedGray[pl].Y),
new PointF(Pt3DashedGray[pl].X, Pt3DashedGray[pl].Y),
new PointF(Pt4DashedGray[pl].X, Pt4DashedGray[pl].Y),
new PointF(Pt5DashedGray[pl].X, Pt5DashedGray[pl].Y),
};
e.Graphics.DrawCurve(Grey, pointsdhg);
e.Graphics.DrawLine(Grey, Pt5DashedGray[pl], Pt6DashedGray[pl]);
}
for (int pl = 0; pl < Pt1SolidGray.Count; pl++)
{
e.Graphics.DrawLine(SolidGrey, Pt1SolidGray[pl], Pt2SolidGray[pl]);
PointF[] pointsdhg = new PointF[]
{
new PointF(Pt2SolidGray[pl].X, Pt2SolidGray[pl].Y),
new PointF(Pt3SolidGray[pl].X, Pt3SolidGray[pl].Y),
new PointF(Pt4SolidGray[pl].X, Pt4SolidGray[pl].Y),
new PointF(Pt5SolidGray[pl].X, Pt5SolidGray[pl].Y),
};
e.Graphics.DrawCurve(SolidGrey, pointsdhg);
e.Graphics.DrawLine(SolidGrey, Pt5SolidGray[pl], Pt6SolidGray[pl]);
}
for (int pl = 0; pl < Pt1DashGreen.Count; pl++)
{
e.Graphics.DrawLine(greenPen, Pt1DashGreen[pl], Pt2DashGreen[pl]);
PointF[] pointsdhg = new PointF[]
{
new PointF(Pt2DashGreen[pl].X, Pt2DashGreen[pl].Y),
new PointF(Pt3DashGreen[pl].X, Pt3DashGreen[pl].Y),
new PointF(Pt4DashGreen[pl].X, Pt4DashGreen[pl].Y),
new PointF(Pt5DashGreen[pl].X, Pt5DashGreen[pl].Y),
};
e.Graphics.DrawCurve(greenPen, pointsdhg);
e.Graphics.DrawLine(greenPen, Pt5DashGreen[pl], Pt6DashGreen[pl]);
}
for (int i = 0; i < Pt1.Count; i++)
{
e.Graphics.DrawLine(SolidGreen, Pt1[i], Pt2[i]);
}
for (int l = 0; l < DashedGreen1.Count; l++)
{
e.Graphics.DrawLine(greenPen, DashedGreen1[l], DashedGreen2[l]);
}
for (int k = 0; k < SolidGrey1.Count; k++)
{
e.Graphics.DrawLine(SolidGrey, SolidGrey1[k], SolidGrey2[k]);
}
for (int j = 0; j < DashedGrey1.Count; j++)
{
e.Graphics.DrawLine(Grey, DashedGrey1[j], DashedGrey2[j]);
}
if (IsDrawing)
{
e.Graphics.DrawLine(Red, NewPt1.X, NewPt1.Y, NewPt2.X, NewPt2.Y);
}
if (Draw_Dashed_Green)
{
e.Graphics.DrawLine(red, DashedGreenPT1, DashedGreenPT2);
}
if (Draw_Solid_Grey)
{
e.Graphics.DrawLine(Red, SolidGreyPT1, SolidGreyPT2);
}
if (Draw_dashed_Grey)
{
e.Graphics.DrawLine(red, DashedGreyPT1, DashedGreyPT2);
}
}
Need Help
Here is my code so far.
BDW, The point of the line is stored in the list from.
The Drawn lines in Paint event also include the line jumpers which has 6 points on it to make a jumps.
So the only problem is I would like to change the Points of the lines as I use the scroll bar.
Thanks for answering my question.
Related
I have a list of signals in a listview. When the user checks one, the values of the signals are being plotted on the chart. Moreover there is a vertical annotation which the user can drag across the graph and see the values for every x value. Each signal has one rectangle annotation that shows the Y value.
My problem is that when the user checks a new signal then the old rectangle annotations do not disappear.
Here is what I mean :
enter image description here
Here is my code so far :
List<RectangleAnnotation> anno = new List<RectangleAnnotation>();
List<Series> checkedItems = new List<Series>();
private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (listView1.FocusedItem != null)
{
double xFactor = 0.03;
double yFactor = 0.02;
CA = chart1.ChartAreas[0];
if (e.NewValue == CheckState.Checked)
{
anno.Clear();
Series s12 = new Series();
s12 = chart1.Series.Add((listView1.Items[e.Index].Text).ToString());
s12.ChartType = SeriesChartType.Line;
s12.MarkerStyle = MarkerStyle.Circle; // make the points stand out
s12.MarkerSize = 3;
checkedItems.Add(s12);
for (int i = 0; i < chart1.Series.Count - 1; i++)
{
anno.Add(new RectangleAnnotation());
anno[i].AxisX = CA.AxisX;
anno[i].IsSizeAlwaysRelative = false;
anno[i].Width = 20 * xFactor;
anno[i].Height = 8 * yFactor;
// VA.Name = "myRect";
anno[i].LineColor = Color.Black;
anno[i].BackColor = Color.Black;
anno[i].AxisY = CA.AxisY;
anno[i].Y = -anno[i].Height;
// RA[i].X = VA.X - RA[i].Width / 2;
anno[i].Text = "Hello";
anno[i].ForeColor = Color.Black;
anno[i].Font = new System.Drawing.Font("Arial", 8f);
anno[i].Text = String.Format("{0:0.00}", 0);
chart1.Annotations.Add(anno[i]);
}
for (int r = 0; r < num_rows; r++)
{
DataPoint dp = new DataPoint();
dp.SetValueXY(r, values[r, listView1.Items.IndexOf(listView1.Items[e.Index])]);
// chart1.Series[checkedListBox1.Items[e.Index].ToString()].Points.Add(dp);
s12.Points.AddXY(r, values[r, listView1.Items.IndexOf(listView1.Items[e.Index])]);
}
}
}
}
private void chart1_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e)
{
int pt1 = (int)e.NewLocationX;
int i = 0;
foreach (var signal in checkedItems) {
double val = signal.Points[pt1].YValues[0];
foreach (var sim in signal.Points[pt1].YValues)
{
anno[i].Y = sim + 0.5;
}
if (sender == VA) anno[i].X = VA.X - anno[i].Width / 2;
anno[i].Text = String.Format("{0:0.00}", val);
i++;
Console.WriteLine(anno.Count);
}
I have thought of adding
chart1.Annotations.clear();
But it deletes all Annotations including the vertical. I only want to delete the rectangle annotations.
I have C# project already done but im having issue with printing it's charts when comes out with more pages of data points, i got the scroll bar work when start getting more pages so the user can review all data on all pages but i could not find how to make the print preview shows them or print them,
when click on print, it shows only the first page on the print preview and same thing when print it out.
her is the print code:
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = this.chart1.Printing.PrintDocument;
((Form)ppd).WindowState = FormWindowState.Maximized;
chart1.Printing.PrintDocument.DefaultPageSettings.Landscape = true;
chart1.Printing.PrintDocument.DefaultPageSettings.Margins.Left = 0;
chart1.Printing.PrintDocument.DefaultPageSettings.Margins.Right = 0;
chart1.Printing.PrintDocument.DefaultPageSettings.Margins.Top = 0;
chart1.Printing.PrintDocument.DefaultPageSettings.Margins.Bottom = 0;
ppd.ShowDialog();
and here is the chart load code:
public void loadChart(string sqlvalue)//load chart method
{
chart1.ChartAreas[0].AxisY.Maximum = 55;
chart1.ChartAreas[0].AxisY.Minimum = 35;
chart1.ChartAreas[0].AxisY.Interval = 5;//control how many lines/Interval
chart1.ChartAreas[0].AxisY.ScrollBar.Enabled = true;
chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisX.Minimum = 0;
// chart1.ChartAreas[0].AxisX.Maximum = 10;
chart1.ChartAreas[0].AxisX.Interval = 1;
//X AXES label angle
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = 60;
da = new SqlDataAdapter(sqlvalue, cn.connect());
da.Fill(dt);
this.chart1.DataSource = dt;
this.chart1.Series["left"].XValueMember = dt.Columns[3].ToString();//date data
this.chart1.Series["left"].YValueMembers = dt.Columns[1].ToString();//spindle 1 data
this.chart1.Series["Right"].YValueMembers = dt.Columns[2].ToString();//spindle 2 data
//label the series lines, Backcolor and forcolor
chart1.Series[0].LabelBackColor = Color.Red;
chart1.Series[0].LabelForeColor = Color.White;
//datapoint marker's color, bordercolor,style and size
chart1.Series[0].MarkerColor = Color.White;
chart1.Series[0].MarkerBorderColor = Color.Black;
chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
chart1.Series[0].MarkerSize = 8;
//datapoint marker's color, style and size
chart1.Series[1].MarkerColor = Color.White;
chart1.Series[1].MarkerBorderColor = Color.Black;
chart1.Series[1].MarkerStyle = MarkerStyle.Circle;
chart1.Series[1].MarkerSize = 8;
chart1.Series[1].LabelBackColor = Color.Blue;
chart1.Series[1].LabelForeColor = Color.White;
//Chart background lines color
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Silver;
chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Silver;
this.chart1.DataBind();
cn.disconnect();
// enable autoscroll
chart1.ChartAreas[0].CursorX.AutoScroll = true;//------------
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(0, 15);
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
// set scrollbar small change to the target size
chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = 15;
Legend left = new Legend();
Legend LC = CustomCloneLegend(chart1, left);
chart1.Legends.Add(LC);
chart1.Padding = Padding.Empty;
ChartArea CA = chart1.ChartAreas[0];
CA.Position = new ElementPosition(4,6, 100, 90);
}
Thank you for your time and help, here is the chart code update:
private static Image MergeImages(List<Image> imageList)
{
var finalSize = new Size();
foreach (var image in imageList)
{
if (image.Width > finalSize.Width)
{
finalSize.Width = image.Width;
}
finalSize.Height += image.Height;
}
var outputImage = new Bitmap(finalSize.Width, finalSize.Height);
using (var gfx = Graphics.FromImage(outputImage))
{
var y = 0;
foreach (var image in imageList)
{
gfx.DrawImage(image, 0, y);
y += image.Height;
}
}
return outputImage;
}
The second method:
List<Image> ChartsToImages(List<Chart> charts)
{
var imageList = new List<Image>();
foreach (var c in charts)
{
using (var ms = new MemoryStream())
{
c.SaveImage(ms, ChartImageFormat.Png);
var bmp = System.Drawing.Bitmap.FromStream(ms);
imageList.Add(bmp);
}
}
return imageList;
}
and this code
var chartList = new List<Chart> { chart1 };
var imageList = ChartsToImages(chartList);
var finalImage = MergeImages(imageList);
finalImage.Save("D:\\Junk.png", ImageFormat.Png);
Im not sure is that what you mean by your first comment, but i found this code here under Converting chart to image questions. this code convert and saves the chart in the same amount of pages but i need to show them in the printpreviewcontrol and print them.
Below code refers to the as per page count starting point and ending point based printing. And Grid view value chars are row loop based counting the page.
private int numberOfItemsPerPage = 0;
private int numberOfItemsPrintedSoFar = 0;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int height = 480; //page height stating point
for (int l = numberOfItemsPrintedSoFar; l < dataGridView2.Rows.Count; l++)
{
numberOfItemsPerPage = numberOfItemsPerPage + 1;
if (numberOfItemsPerPage <= 25) // 25 is Page Line Item
{
numberOfItemsPrintedSoFar++;
if (numberOfItemsPrintedSoFar <= dataGridView2.Rows.Count)
{
height += dataGridView2.Rows[0].Height;
e.Graphics.DrawString(dataGridView2.Rows[l].Cells[0].FormattedValue.ToString(), dataGridView2.Font = new Font("Book Antiqua", 8), Brushes.Black, new RectangleF(5, height, dataGridView2.Columns[0].Width, dataGridView2.Rows[0].Height));
e.Graphics.DrawString(dataGridView2.Rows[l].Cells[1].FormattedValue.ToString(), dataGridView2.Font = new Font("Book Antiqua", 8), Brushes.Black, new RectangleF(170, height, dataGridView2.Columns[0].Width, dataGridView2.Rows[0].Height));
e.Graphics.DrawString(dataGridView2.Rows[l].Cells[2].FormattedValue.ToString(), dataGridView2.Font = new Font("Book Antiqua", 8), Brushes.Black, new RectangleF(290, height, dataGridView2.Columns[0].Width, dataGridView2.Rows[0].Height));
e.Graphics.DrawString(dataGridView2.Rows[l].Cells[3].FormattedValue.ToString(), dataGridView2.Font = new Font("Book Antiqua", 8), Brushes.Black, new RectangleF(345, height, dataGridView2.Columns[0].Width, dataGridView2.Rows[0].Height));
}
else
{
e.HasMorePages = false;
}
}
else
{
numberOfItemsPerPage = 0;
e.HasMorePages = true;
return;
}
}
numberOfItemsPerPage = 0;
numberOfItemsPrintedSoFar = 0;
}
I am trying to cluster(group) every circle that's uninterrupted overlapping (connected) to each other how could I do that? (preferably in a pretty efficient way).
(I have messed around trying to write some recursive functions but haven't gotten anything to work.)
I have created a VS project to visualize the problem.
Download here:
Generates Random circles.
How the clustering currently works:
(its only looks at what circle is overlapping that specific circle not all that is connected)
How it should look if its working
(separate clusters for all connecting circles)
CODE: (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
// Cluster overlapping circles
// Patrik Fröhler
// www.patan77.com
// 2017-08-14
namespace circleGroup
{
struct circle // the circle "object"
{
public float[] pos;
public int radius;
public Color color;
public int id;
public float x
{
get { return pos[0]; }
set { pos[0] = value; }
}
public float y
{
get { return pos[1]; }
set { pos[1] = value; }
}
}
public partial class Form1 : Form
{
DB _DB = new DB(); // "Global Database"
public Form1()
{
InitializeComponent();
}
private static circle createCircle(float _x = 0, float _y = 0, int _radius = 1, Color? _color = null, int _id = -1) // creates a circle
{
circle tmpCircle = new circle() { pos = new float[2], x = _x, y = _y, radius = _radius, id = _id };
tmpCircle.color = _color ?? Color.Black;
return (tmpCircle);
}
private circle[] genRngCircles(int _n) // generates an array of random circles
{
Random rng = new Random();
circle tmpC;
circle[] tmpCarr = new circle[_n];
for (int i = 0; i < _n; i++)
{
tmpC = createCircle();
tmpC.radius = rng.Next(10, 75);
tmpC.x = rng.Next(tmpC.radius, (512 - tmpC.radius));
tmpC.y = rng.Next(tmpC.radius, (512 - tmpC.radius));
tmpC.color = Color.FromArgb(127, rng.Next(0, 255), rng.Next(0, 255), rng.Next(0, 255));
tmpC.id = i;
tmpCarr[i] = tmpC;
}
return tmpCarr;
}
private void drawCircle(circle _circle, Graphics _g) // draws one circle
{
SolidBrush sb = new SolidBrush(_circle.color);
_g.FillEllipse(sb, (_circle.x - _circle.radius), (_circle.y - _circle.radius), (_circle.radius * 2), (_circle.radius * 2));
sb.Dispose();
}
private void drawString(float[] _pos, string _text, Graphics _g) // draws text
{
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
Font font = new Font("Arial", 12);
SolidBrush sb = new SolidBrush(Color.Black);
float x = _pos[0];
float y = _pos[1];
_g.DrawString(_text, font, sb, x, y, sf);
font.Dispose();
sb.Dispose();
}
private void drawCircleArr(circle[] _circleArr, Graphics _g)// draws an array of circles
{
_g.Clear(panel1.BackColor);
for (int i = 0; i < _circleArr.Length; i++)
{
drawCircle(_circleArr[i], _g);
drawString(_circleArr[i].pos, _circleArr[i].id.ToString(), _g);
}
}
static double mDistance<T>(T[] _p0, T[] _p1) // gets euclidean distance between two points of arbitrary numbers of dimensions
{
double[] p0 = new double[] { Convert.ToDouble(_p0[0]), Convert.ToDouble(_p0[1]) };
double[] p1 = new double[] { Convert.ToDouble(_p1[0]), Convert.ToDouble(_p1[1]) };
double tmp = 0;
double tmpTotal = 0;
for (int i = 0; i < _p0.Length; i++)
{
tmp = (p0[i] - p1[i]);
tmpTotal += (tmp * tmp);
}
double output = Math.Sqrt(tmpTotal);
return (output);
}
private bool overlap(circle _c0, circle _c1) // checks if two circles overlap
{
double dis = mDistance(_c0.pos, _c1.pos);
if (dis <= (_c0.radius + _c1.radius))
{
return (true);
}
return (false);
}
private Color avgColor(List<circle> _colorArr) // averages mutiple colors togehter
{
float ia = 0;
float ir = 0;
float ig = 0;
float ib = 0;
for (int i = 0; i < _colorArr.Count; i++)
{
ia += _colorArr[i].color.A;
ir += _colorArr[i].color.R;
ig += _colorArr[i].color.G;
ib += _colorArr[i].color.B;
}
byte a = Convert.ToByte(Math.Round(ia / _colorArr.Count));
byte r = Convert.ToByte(Math.Round(ir / _colorArr.Count));
byte g = Convert.ToByte(Math.Round(ig / _colorArr.Count));
byte b = Convert.ToByte(Math.Round(ib / _colorArr.Count));
return (Color.FromArgb(a, r, g, b));
}
private void treeView(List<circle>[] _circleLArr) // Create Treeview
{
treeView1.Nodes.Clear();
for (int i = 0; i < _circleLArr.Length; i++)
{
treeView1.Nodes.Add(i.ToString());
for (int j = 0; j < _circleLArr[i].Count; j++)
{
treeView1.Nodes[i].Nodes.Add(_circleLArr[i][j].id.ToString());
}
}
treeView1.ExpandAll();
}
private void drawCircleClusters(List<circle>[] _circleLArr, Graphics _g) // draws the circle clusters
{
_g.Clear(panel1.BackColor);
circle tmpC;
Color tmpColor;
for (int i = 0; i < _circleLArr.Length; i++)
{
tmpColor = avgColor(_circleLArr[i]);
for (int j = 0; j < _circleLArr[i].Count; j++)
{
tmpC = _circleLArr[i][j];
tmpC.color = tmpColor;
drawCircle(tmpC, _g);
drawString(_circleLArr[i][j].pos, _circleLArr[i][j].id.ToString(), _g);
}
}
}
//----------------------------------------------------
private List<circle>[] simpleOverlap(circle[] _circleArr) // test what circles overlaps
{
List<circle>[] tmpLArr = new List<circle>[_circleArr.Length];
for (int i = 0; i < (_circleArr.Length); i++)
{
tmpLArr[i] = new List<circle>();
for (int j = 0; j < (_circleArr.Length); j++)
{
if (overlap(_circleArr[i], _circleArr[j]))
{
tmpLArr[i].Add(_circleArr[j]);
}
}
}
return (tmpLArr);
}
/*
private circle[] recurOverlap(circle[] _circleArr) // recursive overlap test(not done/working)
{
List<circle> overlapArr = new List<circle>();
List<circle> dontOverlapArr = new List<circle>();
bool loop = true;
int n = 0;
while (loop)
{
if (overlap(_circleArr[0], _circleArr[n]))
{
overlapArr.Add(_circleArr[n]);
dontOverlapArr.Insert(0, _circleArr[n]);
circle[] dontArr = dontOverlapArr.ToArray();
recurOverlap(dontArr);
}
else
{
dontOverlapArr.Add(_circleArr[n]);
}
n++;
if (n >= _circleArr.Length)
{
loop = false;
}
}
if(_circleArr.Length <= 1)
{
return _circleArr;
}
else{
return overlapArr.ToArray();
}
}
private List<circle>[] clusterBrecur(circle[] _circleArr)
{
List<circle>[] tmpLArr = new List<circle>[_circleArr.Length];
for (int i = 0; i < (_circleArr.Length); i++)
{
tmpLArr[i] = new List<circle>();
recurOverlap(_circleArr);
}
return (tmpLArr);
}*/
private void run() // Run function
{
treeView1.Nodes.Clear(); // clear tree view
_DB.g = panel1.CreateGraphics();// Create Panel Graphics to draw on
_DB.circleArr = genRngCircles(10); // Creates an array with random circles
drawCircleArr(_DB.circleArr, _DB.g); // Draws the random circles
clusterAbtn.Enabled = true; // enables the cluster button
}
private void clusterA() // clusterA function
{
_DB.circleClusters = simpleOverlap(_DB.circleArr); // runs cluster algorithm test A
treeView(_DB.circleClusters); // Creates the treeview
drawCircleClusters(_DB.circleClusters, _DB.g); // draws the circle clusters
}
private void clusterB()
{
}
private void clusterA_rClick()
{
drawCircleArr(_DB.circleArr, _DB.g); // Draws the random circles
}
private void runBtn_Click(object sender, EventArgs e) // run button click
{
run();
}
private void clusterAbtn_MouseUp(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
clusterA();
break;
case MouseButtons.Right:
clusterA_rClick();
break;
}
}
private void clusterBbtn_Click(object sender, EventArgs e) // clusterB button click
{
clusterB();
}
}
class DB // "Database"
{
public Graphics g;
public circle[] circleArr;
public List<circle>[] circleClusters;
}
}
The current "overlap function"
private List<circle>[] simpleOverlap(circle[] _circleArr) // test what circles overlaps
{
List<circle>[] tmpLArr = new List<circle>[_circleArr.Length];
for (int i = 0; i < (_circleArr.Length); i++)
{
tmpLArr[i] = new List<circle>();
for (int j = 0; j < (_circleArr.Length); j++)
{
if (overlap(_circleArr[i], _circleArr[j]))
{
tmpLArr[i].Add(_circleArr[j]);
}
}
}
return (tmpLArr);
}
I made following change to your code. Looks like working
private List<circle>[] simpleOverlap(circle[] _circleArr) // test what circles overlaps
{
List<List<circle>> list = new List<List<circle>>();
//List<circle>[] tmpLArr = new List<circle>[_circleArr.Length];
//for (int i = 0; i < (_circleArr.Length); i++)
foreach (circle circle in _circleArr)
{
List<circle> cluster = null;
//tmpLArr[i] = new List<circle>();
//for (int j = 0; j < (_circleArr.Length); j++)
//{
// if (overlap(_circleArr[i], _circleArr[j]))
// {
// tmpLArr[i].Add(_circleArr[j]);
// }
//}
foreach(List<circle> cluster2 in list)
{
foreach (circle circle2 in cluster2)
{
if (overlap(circle, circle2))
{
cluster = cluster2;
goto label_001;
}
}
}
label_001:
if (cluster == null)
{
cluster = new List<circle>();
list.Add(cluster);
}
cluster.Add(circle);
}
bool flag = true;
for (int i = 0; i < list.Count; i += (flag ? 1 : 0))
{
flag = true;
List<circle> cluster = list[i];
for (int j = i + 1; j < list.Count; j++)
{
List<circle> cluster2 = list[j];
if (Intersects(cluster, cluster2))
{
cluster.AddRange(cluster2);
list.Remove(cluster2);
j--;
flag = false;
}
}
}
return list.ToArray();
//return (tmpLArr);
}
bool Intersects(List<circle> cluster1, List<circle> cluster2)
{
foreach (circle circle1 in cluster1)
{
foreach (circle circle2 in cluster2)
{
if (overlap(circle1, circle2))
{
return true;
}
}
}
return false;
}
I had to add 1 more method bool Intersects(List<circle> cluster1, List<circle> cluster2). See if it helps.
I believe the function you are looking for is intersection. I have attached an article by Mike K which I believe will give you an idea of how to approach this in your own code.
C# circles intersections
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < pala.cantLetras; i++)
{ guiones[i] = new Label();
guiones[i].Text = "_";
guiones[i].Font = new Font("Berlin Sans FB Demi", 25);
guiones[i].Size = new Size(18, 37);
guiones[i].Name = "guion" + i;
guiones[i].ForeColor = Color.Black;
guiones[i].BackColor = Color.Transparent;
guiones[i].Location = new Point(x, 341);
this.Controls.Add(guiones[i]);
recguion[i] = guiones[i].Bounds;
recguion[i] = new Rectangle();
//recguion[i].Location = new Point(x, 341);
x = x + 50;
}
for (int i = 0; i < pala.cantLetras; i++)
{
labels[i] = new Label();
labels[i].Size = new Size(25, 55);
labels[i].Name = "label" + i;
labels[i].Text = pala.palabra[i].ToString();
labels[i].Font = new Font("Berlin Sans FB Demi", 20);
labels[i].ForeColor = Color.Black;
labels[i].BackColor = Color.Transparent;
labels[i].Location = new Point(y, 165);
this.Controls.Add(labels[i]);
posRandom[i] = y;
reclabel[i] = labels[i].Bounds;
reclabel[i] = new Rectangle();
// reclabel[i].Location = new Point(y, 165);
y = y + 40;
}
}
I need to know when reclabel[] intersects the recguion[] corresponding to that number.
Ex: reclabel[1] Intersects recguion[1] but only that one, if it intersects another it has to say that it's wrong.
The rectangles have inside(or that's what I a'm trying) labels[] and guiones[]
This is what I have tryied but it doesnt work.
private void intersecta()
{
int cont = 0;
for (int i = 0; i < pala.cantLetras; i++)
{
for (int j = 0; j < pala.cantLetras; j++)
{
if (i==j)
{
Rectangle intersect = Rectangle.Intersect(reclabel[i], recguion[j]);
if (intersect != Rectangle.Empty)
{
MessageBox.Show("Intersection!");
cont++;
}
}
if (cont != 0)
{
i = pala.cantLetras - 1;
j = pala.cantLetras - 1;
}
}
}
}
Thank you!
There is no need for a nested loop. Just loop through one array and check both rectangles at that index with .IntersectsWith. My apologies if there are any syntax errors, I don't have access to Visual Studio at the moment.
For(int i = 0; i < Array1.Length; i++)
{
if(Array1[i].IntersectsWith(Array2[i]))
{
//Intersected
}
}
But also, as Andrew pointed out, you have a serious problem here:
reclabel[i] = new Rectangle();
You are just overwriting all your data with a new instance (of a different type!).
Graphics graphics = Graphics.FromImage(BitMap_1); // <--This is not allowed? How can I fix it?How can I declare graphics as global?
Here is some more code...
Here is some more code...
Here is some more code...
Here is some more code...
public partial class Form1 : Form
{
string WAV_filePath = #"";
string MIDI_filePath = #"";
string filePath = #"C:\Users\Steffan\Desktop\guitar\Gert toets die Elektroniese Konsertina.wav";//Guitar 2.wav";//Sine Wave 440Hz.wav";
SoundPlayer player1 = new SoundPlayer();
byte[] RawWaveDataArray = new byte[100];
Int16[] Data_16Bit = new Int16[100];
int NumberOfSamples = 0;
bool PLAY_ = false;
Point[] Points = new Point[886];
// Create pen.
Pen Pen_ = new Pen(Color.White, 0);
Bitmap BitMap_1 = new Bitmap(1138, 72);
Graphics graphics = Graphics.FromImage(BitMap_1); // <--This is not allowed? How can I fix it?How can I declare graphics as global?
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (PLAY_ == true)
{
Wave_x_Inc = 0;
graphics.Clear(Color.Black);
for (int x = 0; x < 886; x++)
{
Points[x] = new Point(Wave_x_Inc, (int)((Data_16Bit[DataIndex] * 0.002F) + (pictureBox4.Height / 2)));
Wave_x_Inc = Wave_x_Inc + 2;
DataIndex = DataIndex + 2;
if (DataIndex > Data_16Bit.Length/2) { DataIndex = 0; x = 886; }
}
if (Wave_x_Inc > pictureBox4.Width) { Wave_x_Inc = 0; }
graphics.DrawBeziers(Pen_, Points);
WaveLengthCounter = WaveLengthCounter + 886;
int Temp_Val = WaveLengthCounter / DataLenth_Fraction;
if (Temp_Val <= 300) { trackBar1.Value = Temp_Val; }
else { trackBar1.Value = 0; }
if (WaveLengthCounter > Data_16Bit.Length/2)
{
WaveLengthCounter = 0;
}
pictureBox4.Image = BitMap_1;
}
}
}
Try this, it may help you.
var bitmap = new Bitmap(width, height);
var graphics = Graphics.FromImage(bitmap);
graphics.DrawRectangle(Pens.Black, 0, 0, 10, 10);
bitmap.Save("MyShapes.png");