c# пересечение прямоугольников
Имеется некий набросок для генерации прямоугольников. Каждый дочерний прямоугольник с рандомным углом и меньше родительского в 1,3 раза.
Задача. Чтобы каждый дочерний прямоугольник не вылезал за границы своего родителя( и прородителей).
Реализовал(скрин), но кривовато, т.е не учитываются прородители.
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .csharp.geshi_code {font-family:monospace;} .csharp.geshi_code .imp {font-weight: bold; color: red;} .csharp.geshi_code .kw1 {color: #0600FF;} .csharp.geshi_code .kw2 {color: #FF8000; font-weight: bold;} .csharp.geshi_code .kw3 {color: #008000;} .csharp.geshi_code .kw4 {color: #FF0000;} .csharp.geshi_code .kw5 {color: #000000;} .csharp.geshi_code .co1 {color: #008080; font-style: italic;} .csharp.geshi_code .co2 {color: #008080;} .csharp.geshi_code .co3 {color: #008080;} .csharp.geshi_code .coMULTI {color: #008080; font-style: italic;} .csharp.geshi_code .es0 {color: #008080; font-weight: bold;} .csharp.geshi_code .es_h {color: #008080; font-weight: bold;} .csharp.geshi_code .br0 {color: #000000;} .csharp.geshi_code .sy0 {color: #008000;} .csharp.geshi_code .st0 {color: #666666;} .csharp.geshi_code .st_h {color: #666666;} .csharp.geshi_code .nu0 {color: #FF0000;} .csharp.geshi_code .me1 {color: #0000FF;} .csharp.geshi_code .me2 {color: #0000FF;} .csharp.geshi_code span.xtra { display:block; }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing.Drawing2D;
namespace lab2
{
public partial class Form1 : Form
{
Point center = new Point(400, 300);
PointF[] P = new PointF[4];
PointF[] P_ = new PointF[4];
Random r = new Random();
GraphicsPath myPath = new GraphicsPath();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
float w = 600f;
float h = 300f;
Random r = new Random();
drawRectangle(w, h, 0,true);
for (int i = 0; i < 2; i++)
{
w = (float)(w / 1.3);
h = (float)(h / 1.3);
drawRectangle(w, h, r.Next(0, 181),false);
}
}
public void drawRectangle(float Width, float Height, int A, bool pre_defined)
{
Graphics g = pictureBox1.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
System.Drawing.Brush brush = new System.Drawing.SolidBrush(Color.FromArgb(r.Next(0, 251), r.Next(0, 251), r.Next(0, 251)));
Pen myPen = new Pen(brush, 2);
int x = center.X;
int y = center.Y;
//top right
P[0] = new PointF((float)Math.Round(x - (Width / 2) * Math.Cos(A) + (Height / 2) * Math.Sin(A)), (float)Math.Round(y - (Height / 2) * Math.Cos(A) - (Width / 2) * Math.Sin(A)));
//top left
P[1] = new PointF((float)Math.Round(x + (Width / 2) * Math.Cos(A) + (Height / 2) * Math.Sin(A)), (float)Math.Round(y - (Height / 2) * Math.Cos(A) + (Width / 2) * Math.Sin(A)));
//bottom left
P[2] = new PointF((float)Math.Round(x + (Width / 2) * Math.Cos(A) - (Height / 2) * Math.Sin(A)), (float)Math.Round(y + (Height / 2) * Math.Cos(A) + (Width / 2) * Math.Sin(A)));
//bottom right
P[3] = new PointF((float)Math.Round(x - (Width / 2) * Math.Cos(A) - (Height / 2) * Math.Sin(A)), (float)Math.Round(y + (Height / 2) * Math.Cos(A) - (Width / 2) * Math.Sin(A)));
if (pre_defined)
{
for (int i = 0; i < 4; i++)
P_[i] = P[i];
}
GraphicsPath myPath = new GraphicsPath();
myPath.AddPolygon(P_);
g.SetClip(myPath, CombineMode.Intersect);
myPath.FillMode = FillMode.Winding;
for (int i = 0; i < 4; i++)
P_[i] = P[i];
//g.FillRegion(new SolidBrush(Color.Black), region);
g.FillPolygon(brush, P);
//g.FillPath(new SolidBrush(Color.Black), myPath);
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Invalidate();
myPath = new GraphicsPath();
}
}
}
Либо http://pastebin.com/MKu8PmMM
Задача. Чтобы каждый дочерний прямоугольник не вылезал за границы своего родителя( и прородителей).
Реализовал(скрин), но кривовато, т.е не учитываются прородители.
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .csharp.geshi_code {font-family:monospace;} .csharp.geshi_code .imp {font-weight: bold; color: red;} .csharp.geshi_code .kw1 {color: #0600FF;} .csharp.geshi_code .kw2 {color: #FF8000; font-weight: bold;} .csharp.geshi_code .kw3 {color: #008000;} .csharp.geshi_code .kw4 {color: #FF0000;} .csharp.geshi_code .kw5 {color: #000000;} .csharp.geshi_code .co1 {color: #008080; font-style: italic;} .csharp.geshi_code .co2 {color: #008080;} .csharp.geshi_code .co3 {color: #008080;} .csharp.geshi_code .coMULTI {color: #008080; font-style: italic;} .csharp.geshi_code .es0 {color: #008080; font-weight: bold;} .csharp.geshi_code .es_h {color: #008080; font-weight: bold;} .csharp.geshi_code .br0 {color: #000000;} .csharp.geshi_code .sy0 {color: #008000;} .csharp.geshi_code .st0 {color: #666666;} .csharp.geshi_code .st_h {color: #666666;} .csharp.geshi_code .nu0 {color: #FF0000;} .csharp.geshi_code .me1 {color: #0000FF;} .csharp.geshi_code .me2 {color: #0000FF;} .csharp.geshi_code span.xtra { display:block; }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing.Drawing2D;
namespace lab2
{
public partial class Form1 : Form
{
Point center = new Point(400, 300);
PointF[] P = new PointF[4];
PointF[] P_ = new PointF[4];
Random r = new Random();
GraphicsPath myPath = new GraphicsPath();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
float w = 600f;
float h = 300f;
Random r = new Random();
drawRectangle(w, h, 0,true);
for (int i = 0; i < 2; i++)
{
w = (float)(w / 1.3);
h = (float)(h / 1.3);
drawRectangle(w, h, r.Next(0, 181),false);
}
}
public void drawRectangle(float Width, float Height, int A, bool pre_defined)
{
Graphics g = pictureBox1.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
System.Drawing.Brush brush = new System.Drawing.SolidBrush(Color.FromArgb(r.Next(0, 251), r.Next(0, 251), r.Next(0, 251)));
Pen myPen = new Pen(brush, 2);
int x = center.X;
int y = center.Y;
//top right
P[0] = new PointF((float)Math.Round(x - (Width / 2) * Math.Cos(A) + (Height / 2) * Math.Sin(A)), (float)Math.Round(y - (Height / 2) * Math.Cos(A) - (Width / 2) * Math.Sin(A)));
//top left
P[1] = new PointF((float)Math.Round(x + (Width / 2) * Math.Cos(A) + (Height / 2) * Math.Sin(A)), (float)Math.Round(y - (Height / 2) * Math.Cos(A) + (Width / 2) * Math.Sin(A)));
//bottom left
P[2] = new PointF((float)Math.Round(x + (Width / 2) * Math.Cos(A) - (Height / 2) * Math.Sin(A)), (float)Math.Round(y + (Height / 2) * Math.Cos(A) + (Width / 2) * Math.Sin(A)));
//bottom right
P[3] = new PointF((float)Math.Round(x - (Width / 2) * Math.Cos(A) - (Height / 2) * Math.Sin(A)), (float)Math.Round(y + (Height / 2) * Math.Cos(A) - (Width / 2) * Math.Sin(A)));
if (pre_defined)
{
for (int i = 0; i < 4; i++)
P_[i] = P[i];
}
GraphicsPath myPath = new GraphicsPath();
myPath.AddPolygon(P_);
g.SetClip(myPath, CombineMode.Intersect);
myPath.FillMode = FillMode.Winding;
for (int i = 0; i < 4; i++)
P_[i] = P[i];
//g.FillRegion(new SolidBrush(Color.Black), region);
g.FillPolygon(brush, P);
//g.FillPath(new SolidBrush(Color.Black), myPath);
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Invalidate();
myPath = new GraphicsPath();
}
}
}
Либо http://pastebin.com/MKu8PmMM