Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting lsort-unique-对象的命令_Sorting_Tcl_Rectangles_Itcl - Fatal编程技术网

Sorting lsort-unique-对象的命令

Sorting lsort-unique-对象的命令,sorting,tcl,rectangles,itcl,Sorting,Tcl,Rectangles,Itcl,我有一个矩形列表,如果有重叠的矩形,我需要报告一个错误。 因此,我决定使用lsort-command对列表进行排序,然后比较新列表和旧列表的长度。如果它们不相等,则存在重叠的矩形。 下面是执行此工作的代码: 包需要Itcl ::itcl::类区域{ 公共方法打印{name}{ 将“$name:$x1_$y1_$x2_$y2_2;” } 公共方法X1{}{返回$X1} 公共方法Y1{}{返回$Y1} 公共方法X2{}{返回$X2} 公共方法Y2{}{返回$Y2} #区域的x1坐标 公共变量x1_

我有一个矩形列表,如果有重叠的矩形,我需要报告一个错误。
因此,我决定使用
lsort-command
对列表进行排序,然后比较新列表和旧列表的长度。如果它们不相等,则存在重叠的矩形。

下面是执行此工作的代码:

包需要Itcl
::itcl::类区域{
公共方法打印{name}{
将“$name:$x1_$y1_$x2_$y2_2;”
}
公共方法X1{}{返回$X1}
公共方法Y1{}{返回$Y1}
公共方法X2{}{返回$X2}
公共方法Y2{}{返回$Y2}
#区域的x1坐标
公共变量x1_389;“”
#区域的y1坐标
公共变量y1_1“”
#区域的x2坐标
公共变量x2_389;“”
#该区域的y2坐标
公共变量y2\u3“”
}
#当两个区域相互重叠时,它们将相等
过程比较项{region1 region2}{

return[expr{[$region1 X2]问题出在比较函数中。比较函数需要返回三个可能的值:-1(或者实际上任何小于零的整数),如果第一个值较大,则返回0;如果值相等,则返回1(实际上是大于零的整数)如果第二个值较大。但是您正在使用的
expr
运算符(
谢谢您的回答,这确实很有帮助。只需在程序体中进行更正(请参阅答案中的编辑)。
package require Itcl

    ::itcl::class Region {

        public method print { name } {
            puts "$name: $x1_ $y1_ $x2_ $y2_"
        }

        public method X1     { } { return $x1_ }
        public method Y1     { } { return $y1_ }
        public method X2     { } { return $x2_ }
        public method Y2     { } { return $y2_ }

        # The x1 coordinate of the region
        public variable x1_ ""

        # The y1 coordinate of the region
        public variable y1_ ""

        # The x2 coordinate of the region
        public variable x2_ ""

        # The y2 coordinate of the region
        public variable y2_ ""

    }

    # two regions will be equal <=> when they overlap each other
    proc compareRegs { region1 region2 } {
        return [ expr {[$region1 X2] <= [$region2 X1] || [$region1 Y2] <= [$region2 Y1] } ]
    }

    # reg1 and reg2 don't overlap
    Region reg1
    reg1 configure -x1_ 5.5 -y1_ 5.5014 -x2_ 6.5 -y2_ 5.7014

    Region reg2
    reg2 configure -x1_ 3.567 -y1_ 5.5014 -x2_ 3.767 -y2_ 5.7014

    # reg2 = reg3
    Region reg3
    reg3 configure -x1_ 3.567 -y1_ 5.5014 -x2_ 3.767 -y2_ 5.7014


    # create a usual list
    set myList { reg1 reg2 reg3 }

    # sort the list
    set mySortedList [lsort -unique -command compareRegs $myList]

    puts "start mySortedList"
    foreach reg $mySortedList {
        $reg print "reg"
    }
    puts "end mySortedList"
    # mySortedList = {reg2}

    if { [llength $mySortedList] != [llength $myList] } {
        puts "ERROR: Regions must not overlap"
    }

    # let's see what's going on
    # reg2 < reg1 is true
    puts "result of reg1 < reg2: [compareRegs reg1 reg2]"
    puts "result of reg2 < reg1: [compareRegs reg2 reg1]"
    # reg2 = reg3 is true
    puts "result of reg2 < reg3: [compareRegs reg2 reg3]"
    puts "result of reg3 < reg2: [compareRegs reg3 reg2]"
    # i.e, in sorted list we should have {reg2 reg1}
proc compareRegs { region1 region2 } {
    # Compare the X values by subtracting them from each other
    set cmp [expr {[$region2 X1] - [$region1 X2]}]
    if {$cmp != 0.0} {
        # Convert to an integer (-1 or 1)
        return [expr {$cmp < 0.0 ? -1 : 1}]
    }
    # Compare the Y values by subtracting them from each other
    set cmp [expr {[$region2 Y1] - [$region1 Y2]}]
    if {$cmp != 0.0} {
        # Convert to an integer (-1 or 1)
        return [expr {$cmp < 0.0 ? -1 : 1}]
    }
    # Both equal; return an integer zero
    return 0
}