本文整理汇总了C#中System.Windows.Rect.IntersectsWith方法的典型用法代码示例。如果您正苦于以下问题:C# Rect.IntersectsWith方法的具体用法?C# Rect.IntersectsWith怎么用?C# Rect.IntersectsWith使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Rect
的用法示例。
在下文中一共展示了Rect.IntersectsWith方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Collision
public ObservableCollection<GameObject> Collision(List<GameObject> attackers, List<GameObject> defenders,
ObservableCollection<GameObject> gameObjects, int canvasHeight, out int numberOfCollisions)
{
var collisions = 0;
foreach (var attacker in attackers)
{
var attackerRectangle = new Rect(attacker.Left, attacker.Top,
attacker.ImageWidth, attacker.ImageHeight);
foreach (var defender in defenders)
{
var defenderRectangle = new Rect(defender.Left, defender.Top,
defender.ImageWidth, defender.ImageHeight);
var collisionDetected = attackerRectangle.IntersectsWith(defenderRectangle);
if (collisionDetected)
{
gameObjects.Remove(attacker);
if (defender.Type != GameObjectType.Player)
{
gameObjects.Remove(defender);
}
collisions += 1;
break;
}
}
}
numberOfCollisions = collisions;
return gameObjects;
}
示例2: GetOnScreenPosition
internal static Rect GetOnScreenPosition(Rect floatRect)
{
Rect rect1 = floatRect;
floatRect = floatRect.LogicalToDeviceUnits();
Rect rect2;
Rect rect3;
Screen.FindMaximumSingleMonitorRectangle(floatRect, out rect2, out rect3);
if (floatRect.IntersectsWith(rect3))
return rect1;
Screen.FindMonitorRectsFromPoint(NativeMethods.NativeMethods.GetCursorPos(), out rect2, out rect3);
var rect4 = rect3.DeviceToLogicalUnits();
if (rect1.Width > rect4.Width)
rect1.Width = rect4.Width;
if (rect1.Height > rect4.Height)
rect1.Height = rect4.Height;
if (rect4.Right <= rect1.X)
rect1.X = rect4.Right - rect1.Width;
if (rect4.Left > rect1.X + rect1.Width)
rect1.X = rect4.Left;
if (rect4.Bottom <= rect1.Y)
rect1.Y = rect4.Bottom - rect1.Height;
if (rect4.Top > rect1.Y + rect1.Height)
rect1.Y = rect4.Top;
return rect1;
}
示例3: Check_Collision
public void Check_Collision()
{
for (int i = 0; i < _lstVM.Count; i++)
{
Elipse el1 = _lstVM[i].El;
var x1 = el1.Left;
var y1 = el1.Top;
Rect r1 = new Rect(x1 + 3, y1 + 3, el1.Width - 3, el1.Height - 3);
for (int j = i + 1; j < _lstVM.Count; j++)
{
Elipse el2 = _lstVM[j].El;
var x2 = el2.Left;
var y2 = el2.Top;
Rect r2 = new Rect(x2 + 3, y2 + 3, el2.Width - 3, el2.Height - 3);
if (r1.IntersectsWith(r2))
{
MessageBox.Show("Game Over:\nCant Elim: "+ _cantElim.ToString());
game_timer.Stop();
list_timer.Stop();
var mainWindow = (Application.Current.MainWindow as Window1);
mainWindow.Close();
return;
//int a = 1;
}
}
}
}
示例4: Apply
/// <summary>
/// Applies the specified old data rect.
/// </summary>
/// <param name="oldDataRect">The old data rect.</param>
/// <param name="newDataRect">The new data rect.</param>
/// <param name="viewport">The viewport.</param>
/// <returns></returns>
///
public override Rect Apply(Rect oldDataRect, Rect newDataRect, Viewport2D viewport)
{
DataRect res = domain;
if (domain.IsEmpty)
{
res = newDataRect;
}
else if (newDataRect.IntersectsWith(domain))
{
res = newDataRect;
if (newDataRect.Size == oldDataRect.Size)
{
if (res.XMin < domain.XMin) res.XMin = domain.XMin;
if (res.YMin < domain.YMin) res.YMin = domain.YMin;
if (res.XMax > domain.XMax) res.XMin += domain.XMax - res.XMax;
if (res.YMax > domain.YMax) res.YMin += domain.YMax - res.YMax;
}
else
{
newDataRect.Intersect(domain);
res = newDataRect;
}
}
return res;
}
示例5: OnScrollChanged
protected override void OnScrollChanged(ScrollChangedEventArgs e)
{
base.OnScrollChanged(e);
var panel = Content as Panel;
if (panel == null)
{
return;
}
var viewport = new Rect(new Point(0, 0), RenderSize);
foreach (UIElement child in panel.Children)
{
if (!child.IsVisible)
{
SetIsInViewport(child, false);
continue;
}
var transform = child.TransformToAncestor(this);
var childBounds = transform.TransformBounds(new Rect(new Point(0, 0), child.RenderSize));
SetIsInViewport(child, viewport.IntersectsWith(childBounds));
}
}
示例6: GetLinkLengthBetweenRectangles
public static double GetLinkLengthBetweenRectangles(Rect rectangle1, Rect rectangle2)
{
if (rectangle1.IntersectsWith(rectangle2)) {
return 0;
}
Point linkStart, linkEnd;
ComputeLinkBetweenRectangles(rectangle1, rectangle2, 0, out linkStart, out linkEnd);
Vector link = linkEnd - linkStart;
return link.Length;
}
示例7: textBox_TextChanged
private void textBox_TextChanged(object sender,TextChangedEventArgs e) {
story.SelectAll();
if(textBox.MaxLength == textBox.Text.Length && textBox.Text == story.Selection.Text.Replace("\r", "").Replace("\n", "")) {
ExportData();
Application.Current.Shutdown();
}
foreach(TextChange i in e.Changes) {
if (textBox.Text.Substring(i.Offset, i.AddedLength) == " ")
{
mCurrentChar = -1;
int index = story.Selection.Text.Substring(i.Offset + 1).IndexOf(' ');
if (index > -1)
{
Int32 length = story.Selection.Text.Substring(i.Offset + 1, index).Length;
mTriggerChar = mRandom.Next(0, length);
mData.Add(mTimer.ElapsedMilliseconds);
mTimer.Restart();
}
}
}
story.Selection.ApplyPropertyValue(RichTextBox.ForegroundProperty,Brushes.Black);
string textToPos = textBox.Text;
string textToRetypeToPos = story.Selection.Text.Substring(0,textToPos.Length);
story.Selection.Select(story.Selection.Start,story.Selection.Start.GetPositionAtOffset(textToPos.Length));
if(textToPos == textToRetypeToPos) story.Selection.ApplyPropertyValue(RichTextBox.ForegroundProperty,Brushes.Green);
else story.Selection.ApplyPropertyValue(RichTextBox.ForegroundProperty,Brushes.Red);
if(mCurrentChar == mTriggerChar) {
popup_image.Source = (BitmapImage)Resources[mRandom.Next(1,6).ToString()];
Rect rect = textBox.GetRectFromCharacterIndex(textBox.CaretIndex,true);
Point coords = textBox.TransformToAncestor(this).Transform(rect.Location);
popup_image.Height = popup_image.Width = mRandom.Next(100,351);
Rect image;
do {
Int32 x = mRandom.Next(0,1281);
Int32 y = mRandom.Next(0,721);
popup.Margin = new Thickness(-popup_image.Width / 2 + x,-popup_image.Height / 2 + textBox.FontSize / 2 + y,0,0);
rect = new Rect(new Point(coords.X - popup_image.Width / 2,coords.Y - popup_image.Height / 2),new Point(coords.X + popup_image.Width / 2,coords.Y + textBox.FontSize + popup_image.Height / 2));
image = new Rect(new Point(popup.Margin.Left,popup.Margin.Top),popup_image.DesiredSize);
} while(popup.Margin.Left + popup_image.Width / 2 < 0 || popup.Margin.Left + popup_image.Width / 2 > 640
|| popup.Margin.Top + popup_image.Height / 2 < 0 || popup.Margin.Top + popup_image.Height / 2 > 480 || rect.IntersectsWith(image));
popup.Visibility = Visibility.Visible;
}
++mCurrentChar;
}
示例8: Start
public void Start()
{
var upSpeed = App.Random.Next(25000, 60000) / 10000.0;
this.timer = new DispatcherTimer(DispatcherPriority.Render, App.Current.Dispatcher);
this.timer.Interval = TimeSpan.FromTicks(10000000 / 60); // 60 loop per sec
this.timer.Tick += (_, __) =>
{
this.right += 10;
this.UI.SetValue(Canvas.RightProperty, this.right);
this.bottom += upSpeed;
this.UI.SetValue(Canvas.BottomProperty, this.bottom);
upSpeed -= 0.1;
var myPoint = this.GetPoint();
var myRect = new Rect(myPoint.X, myPoint.Y, this.UI.ActualWidth, this.UI.ActualHeight);
var match = this.field.Walls.Cast<WithHitPoints>()
.Concat(new WithHitPoints[] { this.field.Castle })
.Where(x => x != null)
.FirstOrDefault(x =>
{
var point = x.GetPoint();
return myRect.IntersectsWith(new Rect(point.X, point.Y, x.UI.ActualWidth, x.UI.ActualHeight));
});
if (match != null)
{
this.timer.Stop();
match.MakeDamage(this.Power);
this.UI = new UnitViews.Explosion();
this.UI.SetValue(Canvas.RightProperty, this.right);
this.UI.SetValue(Canvas.BottomProperty, this.bottom);
if (this.UIChanged != null)
this.UIChanged(this, EventArgs.Empty);
DispatcherAsync.Delay(TimeSpan.FromSeconds(0.5), () =>
{
if (this.Disappeared != null)
this.Disappeared(this, EventArgs.Empty);
});
}
else if (this.right > (this.UI.Parent as FrameworkElement).ActualWidth)
{
this.timer.Stop();
if (this.Disappeared != null)
this.Disappeared(this, EventArgs.Empty);
}
};
this.timer.Start();
}
示例9: FindInArea
public virtual IEnumerable<int> FindInArea(Rect area)
{
for (var i = 0; i < Count; i++)
{
var x = XValues[i];
var y = YValues[i];
var w = WidthValues[i];
var h = HeightValues[i];
var rect = new Rect(x, y, w, h);
if (area.IntersectsWith(rect))
{
yield return i;
}
}
}
示例10: IsFree
bool IsFree(MyObject obj, int x, int y)
{
Rect myRectangleObj = new Rect();
myRectangleObj.Location = new Point(x, y);
myRectangleObj.Size = new Size(obj.Width, obj.Height);
foreach (var item in area.LMyObject)
{
Rect myRectangle = new Rect();
myRectangle.Location = new Point(item.X, item.Y);
myRectangle.Height = item.Height;
myRectangle.Width = item.Width;
if (myRectangle.IntersectsWith(myRectangleObj))
return false;
}
return true;
}
示例11: Apply
public override Rect Apply(Rect oldDataRect, Rect newDataRect, Viewport2D viewport)
{
Rect res = oldDataRect;
if (newDataRect.IntersectsWith(border))
{
res = newDataRect;
if (newDataRect.Size == oldDataRect.Size)
{
if (res.X < border.X) res.X = border.X;
if (res.Y < border.Y) res.Y = border.Y;
if (res.Right > border.Right) res.X += border.Right - res.Right;
if (res.Bottom > border.Bottom) res.Y += border.Bottom - res.Bottom;
}
else
{
res = Rect.Intersect(newDataRect, border);
}
}
return res;
}
示例12: OnSetIsUserVisibleBehaviourChanged
private static void OnSetIsUserVisibleBehaviourChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
{
FrameworkElement frameworkElement = dependencyObject as FrameworkElement;
if (frameworkElement == null)
return;
if (!(eventArgs.NewValue is bool))
return;
if ((bool)eventArgs.NewValue)
{
frameworkElement.LayoutUpdated += (sender, layoutUpdateEventArgs) =>
{
if (frameworkElement != null)
{
//HACK: AJ: This uses a hard coded container type.
FrameworkElement containerFrameworkElement = frameworkElement.FindAncestor<System.Windows.Controls.ListView>();
bool isUserVisible = false;
if (frameworkElement.IsVisible)
{
Rect containerBounds = frameworkElement.TransformToAncestor(containerFrameworkElement).TransformBounds(new Rect(0.0, 0.0, frameworkElement.ActualWidth, frameworkElement.ActualHeight));
Rect frameworkElementBounds = new Rect(0.0, 0.0, containerFrameworkElement.ActualWidth, containerFrameworkElement.ActualHeight);
isUserVisible = frameworkElementBounds.IntersectsWith(containerBounds);
}
if (GetIsUserVisible(frameworkElement) != isUserVisible)
frameworkElement.SetValue(IsUserVisibleProperty, isUserVisible);
}
};
frameworkElement.Unloaded += (sender, routedEventArgs) =>
{
frameworkElement.SetValue(IsUserVisibleProperty, false);
};
}
}
示例13: IsFullyOrPartiallyVisible
protected bool IsFullyOrPartiallyVisible(FrameworkElement child, FrameworkElement scrollViewer)
{
var childTransform = child.TransformToAncestor(scrollViewer);
var childRectangle = childTransform.TransformBounds(
new Rect(new Point(0, 0), child.RenderSize));
var ownerRectangle = new Rect(new Point(0, 0), scrollViewer.RenderSize);
return ownerRectangle.IntersectsWith(childRectangle);
}
示例14: IntersectsWith
public void IntersectsWith ()
{
Rect r = new Rect (0, 0, 50, 50);
// fully contained
Assert.IsTrue (r.IntersectsWith (new Rect (10, 10, 10, 10)));
// crosses top side
Assert.IsTrue (r.IntersectsWith (new Rect (5, -5, 10, 10)));
// crosses right side
Assert.IsTrue (r.IntersectsWith (new Rect (5, 5, 50, 10)));
// crosses bottom side
Assert.IsTrue (r.IntersectsWith (new Rect (5, 5, 10, 50)));
// crosses left side
Assert.IsTrue (r.IntersectsWith (new Rect (-5, 5, 10, 10)));
// completely outside (top)
Assert.IsFalse (r.IntersectsWith (new Rect (5, -5, 1, 1)));
// completely outside (right)
Assert.IsFalse (r.IntersectsWith (new Rect (75, 5, 1, 1)));
// completely outside (bottom)
Assert.IsFalse (r.IntersectsWith (new Rect (5, 75, 1, 1)));
// completely outside (left)
Assert.IsFalse (r.IntersectsWith (new Rect (-25, 5, 1, 1)));
}
示例15: OverlapBox
/// <summary>
/// Checks for shape overlap of bounding boxes (collision detection).
/// </summary>
/// <param name="shape1">
/// The first shape name.
/// </param>
/// <param name="shape2">
/// The second shape name.
/// </param>
/// <returns>
/// "True" or "False".
/// </returns>
public static Primitive OverlapBox(Primitive shape1, Primitive shape2)
{
UIElement obj1, obj2;
try
{
if (!_objectsMap.TryGetValue((string)shape1, out obj1))
{
Utilities.OnShapeError(Utilities.GetCurrentMethod(), shape1);
return "False";
}
if (!_objectsMap.TryGetValue((string)shape2, out obj2))
{
Utilities.OnShapeError(Utilities.GetCurrentMethod(), shape2);
return "False";
}
Rect rect1 = new Rect(obj1.RenderSize);
Rect rect2 = new Rect(obj2.RenderSize);
rect1.Offset(VisualTreeHelper.GetOffset(obj1));
rect2.Offset(VisualTreeHelper.GetOffset(obj2));
return rect1.IntersectsWith(rect2) ? "True" : "False";
}
catch (Exception ex)
{
Utilities.OnError(Utilities.GetCurrentMethod(), ex);
return "False";
}
}