繁体   English   中英

如何获取 dm-script 行注释(组件)的起点/终点?

[英]How to get start/end point of a dm-script line annotation (Component)?

我想使用:

Component NewLineAnnotation( Number top, Number left, Number bottom, Number right )

问题是这条线总是在从左上角到右下角的方向上运行。

但是,我需要这条线从右下角到左上角。 而且我还需要能够从现有的方向获得准确的方向。

命令不是:
Component NewLineAnnotation( Number start_x, Number start_y, Number end_x, Number end_y )

但肯定有办法获得线注释的方向吗?

void ComponentGetRect( Component comp, NumberVariable top, NumberVariable left, NumberVariable bottom, NumberVariable right )

没有这样的细节。

鉴于较早的出色答案中的详细信息,我查看了更多内容,发现情况确实如此-我正在使用 GMS 3。

通过以下方式将线创建为组件的命令:

NewLineAnnotation( Number top, Number left, Number bottom, Number right )

可以更好地描述为:

NewLineAnnotation( Number sX, Number sY, Number eX, Number eY )

但还有另一个潜在的混乱来源。 在查找坐标的 Get() 命令时,列出了这些命令:

void ComponentGetBoundingRect( Component comp, NumberVariable t, NumberVariable l, NumberVariable b, NumberVariable r )

void ComponentGetRect( Component comp, NumberVariable top, NumberVariable left, NumberVariable bottom, NumberVariable right ) 

void ComponentGetRectInView( Component comp, NumberVariable top, NumberVariable left, NumberVariable bottom, NumberVariable right )

所有 3 个命令都声明顺序:上、左、下、右,但只有第一个和最后一个命令可以。 第二个命令可以再次更好地描述为

ComponentGetRect(Number sX, Number sY, Number eX, Number eY)

我将命令 NewLineAnnotation() 与 ComponentGetBoundingRect() 混合在一起,这导致了一些实际问题。 我觉得手册中更好的描述可能有助于避免此类问题。

作为总结,这里有一些代码我用来向自己演示不同的功能。

image front := CreateFloatImage("", 800, 800)
front.setZoom(.4)
front.ShowImage()
component frontComponent = front.ImageGetImageDisplay(0)

component solid_Line_orange = NewLineAnnotation(700, 10, 500, 200);
frontComponent.ComponentAddChildAtEnd(solid_Line_orange)
solid_Line_orange.ComponentSetForegroundColor(1,.66,0);
component solid_Line_white  = NewLineAnnotation(500, 10, 700, 200);
frontComponent.ComponentAddChildAtEnd(solid_Line_white)

number sx, sy, ex, ey
number l_top, l_left, l_buttom, l_right
solid_Line_orange.ComponentGetRect( l_top, l_left, l_buttom, l_right );

Result("\n\n")
Result("using:  ComponentGetRectangle();\t actual convention: (start_x, start_y, end_x, end_y)\n")
Result("orange line set at: \t(700, 10, 500, 200);\n")
Result("orange line found at: \t(" + l_top + ", " +  l_left + ", " + l_buttom + ", " + l_right + ")\n")
solid_Line_white.ComponentGetRect( l_top, l_left, l_buttom, l_right ) 
Result("white line set at: \t(500, 10, 700, 200); \n")
Result("white line found at: \t(" + l_top + ", " +  l_left + ", " + l_buttom + ", " + l_right + ")\n")

Result("using:  ComponentGetBoundingRectangle();\t actual convention: (top, left, bottom, right)\n")
solid_Line_orange.ComponentGetBoundingRect( l_top, l_left, l_buttom, l_right ); 
Result("orange line found at: \t(" + l_top + ", " +  l_left + ", " + l_buttom + ", " + l_right + ")\n")
solid_Line_white.ComponentGetBoundingRect( l_top, l_left, l_buttom, l_right ); 
Result("white line found at: \t(" + l_top + ", " +  l_left + ", " + l_buttom + ", " + l_right + ")\n")

Result("using:  ComponentGetBoundingRectInView();\t actual convention: (top, left, bottom, right)\n")
solid_Line_orange.ComponentGetBoundingRectInView( l_top, l_left, l_buttom, l_right ); 
Result("orange line found at: \t(" + l_top + ", " +  l_left + ", " + l_buttom + ", " + l_right + ")\n")
solid_Line_white.ComponentGetBoundingRectInView( l_top, l_left, l_buttom, l_right ); 
Result("white line found at: \t(" + l_top + ", " +  l_left + ", " + l_buttom + ", " + l_right + ")\n")

这为我解决了问题。 希望这对其他人也有帮助。 也许手册中更好的描述也会有所帮助。

这是一个有趣的问题! 我认为ComponentGetRect()的行为正在做正确的事情,但错误地标记了文档中的变量名称。 虽然它说它返回一个矩形作为左上角到右下角的坐标,但它实际上返回的是注释的 Y/X 开始坐标和 Y/X 结束坐标对。 (对于其他类型的注释,这就是 [TLBR] )。 请注意,它是 Y/X 而不是 X/Y! 这令人困惑,但要使其与更一般的“组件”对象保持一致。

同样, NewLineAnnotation()的签名变量名称是错误的,因为它们很明显地给出了开始到结束的方向,因为您可以在使用箭头注释时轻松尝试。 (请参阅下面的示例脚本。)

您可以明确地使用以下方法设置创建的线或箭头注释的方向:

Component NewLineAnnotation( Number StartY, Number StartX, Number EndY, Number EndX )
Component NewArrowAnnotation( Number StartY, Number StartX, Number EndY, Number EndX )

同样,您应该用于阅读职位:
ComponentGetRect( NumberVariable StartY, NumberVariable StartX, NumberVariable EndY, NumberVariable EndX )

现在,获取开始和结束坐标的替代方法是组件的控制点,F1 帮助文档明确列出了控制点 ID 值: 在此处输入图片说明

因此,大概正确的方法是使用以下命令询问指定控制点坐标的组件:

Boolean ComponentGetControlPoint( Component comp, Number loc, NumberVariable x, NumberVariable y )

但是,对此进行测试(请参阅下面的脚本)它实际上不适用于控制点ID 值12 似乎没有用命令实现一行开始/结束的两个控制点。

但是,您可以使用“上/左”控制点4获取起始值,使用“下/右”控制点7获取结束值。 同样,这只是与常规组件上“线/箭头”注释的相同有点不规则的映射相匹配。

获取起点 XY 的替代方法:
Boolean ComponentGetControlPoint( Component comp, 4, NumberVariable x, NumberVariable y )

获取终点 XY 的替代方法:
Boolean ComponentGetControlPoint( Component comp, 7, NumberVariable x, NumberVariable y )


使用的测试脚本

// Control Point constants. See F1 help documentaton ("Component Object" Overview)
number kLineStart   = 1 // = start point of line
number kLineEnd     = 2 // = end point of line
number kTopLeft     = 4 // = top-left for rect, or start point for line
number kBottomRight = 7 // = bottom-right for rect, or end point for line

number sx = 50
number sy = 30
number ex = 400
number ey = 200
number sizeX = 500
number sizeY = 500

void PrintAnnoInfo( component anno )
{
    if ( !anno.ComponentIsValid() ) return
    number t,l,b,r
    anno.ComponentGetRect( t, l, b, r ) 
    // note that it is return StartY, StartX, EndY, EndX into TLBR!
    
    number type = anno.ComponentGetType()
    Result( "\n\n Component of type " + type + ":" )
    Result( "\n\t Position rect: " + t + " / " + l + " / "+ b + " / "+ r )
    Result( "\n\t\t { TOP / LEFT / BOTTOM / RIGHT }" )
    
    number sx, sy, ex, ey
    if ( !anno.ComponentGetControlPoint( kLineStart, sx, sy ) )
        Result( "\n INVALID Control point for annotation type. ( " + kLineStart + " ) " )
    else
        Result( "\n\t Start point (X/Y): " + sx + " / " + sy )
    if ( !anno.ComponentGetControlPoint( kLineEnd, ex, ey ) )
        Result( "\n INVALID Control point for annotation type. ( " + kLineEnd + " ) " )
    else
        Result( "\n\t End point (X/Y)  : " + ex + " / " + ey )

    if ( !anno.ComponentGetControlPoint( kTopLeft, sx, sy ) )
        Result( "\n INVALID Control point for annotation type. ( " + kLineStart + " ) " )
    else
        Result( "\n\t Start point (X/Y): " + sx + " / " + sy )
    if ( !anno.ComponentGetControlPoint( kBottomRight, ex, ey ) )
        Result( "\n INVALID Control point for annotation type. ( " + kLineEnd + " ) " )
    else
        Result( "\n\t End point (X/Y)  : " + ex + " / " + ey )
    
Result( "\n\n" )
    
}

// clear workspace and created dummy
EGUPerformActionWithAllShownImages( "Delete" )
ClearResults()
Result( "Component direction test:\n\n" )
image img := Realimage( "Test", 4, sizeX, sizeY )
img.ShowImage()
imageDisplay disp = img.ImageGetImageDisplay(0)

// Creating and adding line-Annotation
number UseArrow
useArrow = TwoButtonDialog( "In this test use", "Arrows", "Lines" )
string msg
msg = "\nAdding " + (useArrow?"arrow":"line") + ":"
msg += "\n from (" + sx + " / " + sy + ") to (" + ex + " / " + ey + ")"
msg += "\n { from (Ex/Ey) to (Sx/Sy) }"
Result( msg )
OKdialog( msg )

Component anno
if ( useArrow )
    anno = NewArrowAnnotation( sy, sx, ey, ex ) // note that it is YX!
else
    anno = NewLineAnnotation( sy, sx, ey, ex )  // note that it is YX!

anno.ComponentSetForegroundcolor(1,0,0)
disp.ComponentAddChildAtEnd( anno )
anno.PrintAnnoInfo()

msg = "\nAdding " + (useArrow?"arrow":"line") + ":"
msg += "\n from (" + ex + " / " + ey + ") to (" + sx + " / " + sy + ")"
msg += "\n { from (Ex/Ey) to (Sx/Sy) }"
Result( msg )
OKdialog( msg )

if ( useArrow )
    anno = NewArrowAnnotation( ey, ex, sy, sx ) // note that it is YX!
else
    anno = NewLineAnnotation( ey, ex, sy, sx )  // note that it is YX!


anno.ComponentSetForegroundcolor(0,1,0)
disp.ComponentAddChildAtBeginning( anno )
anno.PrintAnnoInfo()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM