gpt4 book ai didi

java - C# 中的 Rectangle2D .outcode 等效吗?

转载 作者:行者123 更新时间:2023-12-01 10:11:43 24 4
gpt4 key购买 nike

正如标题所说:C#中Java的.outcode是什么?

Java:

public static final Rectangle2D.Double gameField 
= new Rectangle2D.Double(18, 18, 764, 564);

gameField.outcode(something)

C#:

public RectangleF gameField= new RectangleF(18, 18, 764, 564);
gameField.???

最佳答案

由于 .NET RectangleF 没有这个 outcode(),所以我编写了一个本质上相同的代码。

public enum RectOut
{
Left = 1,
Top = 2,
Right = 4,
Bottom = 8,
}

public static class RectangleFExtensions
{
public static int Outcode(this RectangleF rect, PointF point)
{
int outcode = 0;

if (rect.Width <= 0) outcode |= (int)RectOut.Left | (int)RectOut.Right;
if (rect.Height <= 0) outcode |= (int)RectOut.Top | (int)RectOut.Bottom;
if (point.Y < rect.Top) outcode |= (int)RectOut.Top;
if (point.Y > rect.Bottom) outcode |= (int)RectOut.Bottom;
if (point.X < rect.Left) outcode |= (int)RectOut.Left;
if (point.X > rect.Right) outcode |= (int)RectOut.Right;

return outcode;
}
}

用法:

RectangleF rect = new RectangleF(18f, 18f, 764f, 564f);
int outcode = rect.Outcode(new PointF(18f, 18f));

这是一个demo

编辑:显然,当该点位于右下角时,RectangleF.Contains 返回 false。更新了代码以不使用 Contains()

关于java - C# 中的 Rectangle2D .outcode 等效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36089804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com