0

I'm doing a project in which you should be able to create shapes in a windows forms environment. I've got two different shapes at the moment, called Circle1 and Rectangle1, they are just what they are called an have similiar properties.

type Rectangle1(x:int, y:int,brush1)=
  let mutable thisx = x
  let mutable thisy = y
  let mutable thiswidth = 50
  let mutable thisheight = 20
  let mutable brush = brush1
  member obj.x with get () = thisx and set x = thisx <- x
  member oby.y with get () = thisy and set y = thisy <- y
  member obj.brush1 with get () = brush and set brush1 = brush <- brush1
  member obj.width with get () = thiswidth and set width = thiswidth <- width
  member obj.height with get () = thisheight and set height = thisheight <- height
  member obj.draw(g:Graphics) = g.FillRectangle(brush,thisx,thisy,thiswidth,thisheight)

This rectangle is clickable and moveable, but I've encountered a problem. I need some kind of method that is similar to the c# bringToFront() method. So that when I click a shape, my shape goes to the front of all other shapes.

My storage list looks like this:

let mutable RectangleList:List<Rectangle1> =  []

And i use a hittest to determine whether the user hit a shape or not:

let rec VilketObjRec (e:MouseEventArgs) (inputlist:List<Rectangle1>) = 
match inputlist with
     |[] -> None
     |head::tail -> if (((e.X >= head.x) && (e.X <= (head.x + head.width))) && (e.Y >= head.y) && (e.Y <= (head.y+head.height))) 
                       then Some(head) else VilketObjRec e tail

Anyone got any kind of idea how to tackle this problem? Cause frankly, I'm lost.

4

3 回答 3

3

根据命中测试功能,您RectangleList存储矩形的顺序似乎与它们在屏幕上出现的顺序相反(开始时的矩形首先被命中测试,因此它将是顶部的那个绘画)。

在这种情况下,如果你想把一个矩形放到顶部,你只需要把它移到列表的开头。您可以在开头创建一个具有指定值的新列表,然后使用以下命令从列表的其余部分中删除该值filter

let BringToFront value list = 
  value :: (List.filter (fun v -> v <> value) list)

该函数仅适用于列表,因此这里是一个使用整数的示例:

BringToFront 3 [ 1;2;3;4 ] = [3;1;2;4]
于 2013-01-05T22:17:32.803 回答
2

Wmeyer 和 Tomas 的答案很好地满足了您对相对较小的矩形集的要求。如果您将使用 10^3 或更多的矩形,并且您在 GUI 启动之前知道它们的坐标,则有简单的静态结构,请在此处输入链接描述。在更复杂的情况下,Hanan Samet 的“空间数据结构的设计与分析”的第 3 章是你最好的朋友。

于 2013-01-07T16:15:48.603 回答
1

基本思想:你可以z给类添加一个坐标Rectangle1。当一个矩形被击中时,确保它获得最高z值。在绘制矩形之前,请确保它们按升序排序z

于 2013-01-05T19:49:27.977 回答