【问题标题】:A script to draw, fill and group triangles in Livecode?在 Livecode 中绘制、填充和分组三角形的脚本?
【发布时间】:2013-08-01 14:07:14
【问题描述】:

感谢您提供 Livecode 脚本块以绘制和填充一组等边三角形的任何帮助。

我正在开发一个开源应用程序,它可以帮助人们创建和分享具有分形图案的故事。

一个关键的挑战是绘制三角形来代表故事的以下元素:

  • 吸引子
  • 挑战
  • 机会(解决紧张局势的状态变化)
  • 战略
  • 测试
  • 决定

上述六个标准故事元素中的每一个都将在应用程序中显示为等边三角形。反过来,每个元素都将与一种独特的颜色相关联——黄色、红色、橙色、紫色、蓝色或绿色。

我希望使用 Livecode 脚本来绘制六个组合在一起的三角形(很像饼片),形成一个六边形来代表整个故事。

每个彩色片段的透明度(混合级别)将表明故事的作者(或受邀审稿人)认为故事的该元素是完整的程度。

我希望在 Livecode 中编写一个脚本:

  • 快速画出六个三角形,组成六边形

  • 用相关的颜色填充每个三角形(每种颜色的初始混合级别几乎是透明的 90%)

  • 根据填充颜色的名称为六个三角形中的每一个分配一个唯一的短名称

  • 将六个三角形组合在一起,以便将它们一起拖动到屏幕上的新位置。

是否有任何脚本(或块)可以帮助解决这个问题?非常感谢任何有助于缩短我的 Livecode 学习曲线的示例代码或链接。

最好的,

马克弗雷泽

====== 进度更新! ====== [8 月 2 日,东部时间下午 6 点]

我刚刚找到并改编了由大学的 Lloyd Rieber 编写的多边形生成脚本。创造六边形的乔治亚州。有没有办法调整它,使它可以创建一个等边三角形,然后可以复制和旋转以填充六边形?

on mouseUp
global tpoints
if exists(grc "HexagonCanvas" of this card) then delete grc "HexagonCanvas"
lock screen
create grc "HexagonCanvas" 
set the loc of grc "HexagonCanvas" to "140,140"
set the opaque of grc "HexagonCanvas" to true
-- resize the new grc
get the rect of grc "HexagonCanvas" 
add 80 to item 4 of it
set the rect of grc "HexagonCanvas" to it
put the topleft of grc "HexagonCanvas" into TL
put the topright of grc "HexagonCanvas" into TR
put the bottomleft of grc "HexagonCanvas" into BL
put the bottomright of grc "HexagonCanvas" into BR
put the width of grc "HexagonCanvas" into twidth
put the height of grc "HexagonCanvas" into theight
put trunc(twidth/4) into twidthquart
put trunc(theight/2) into theighthalf
#=========set the points for the "free" hexagon polygon==================
put empty into tpoints
put (item 1 of TL + twidthquart, item 2 of TL) into tpoints
# for the first line of tpoints "put into"
put Cr& (item 1 of TL, item 2 of TL + theighthalf) after tpoints
put CR& (item 1 of BL + twidthquart, item 2 of BL) after tpoints
put CR& (item 1 of BR - twidthquart, item 2 of BR) after tpoints
put Cr& (item 1 of BR, item 2 of BR - theighthalf) after tpoints
put CR& (item 1 of TR - twidthquart, item 2 of TR) after tpoints
put CR& (item 1 of TL + twidthquart, item 2 of TL) after tpoints
set the points of grc "HexagonCanvas" to tpoints
set the style of grc "HexagonCanvas" to "polygon"
set the backgroundColor of grc "HexagonCanvas" to blue
set the blendlevel of grc "HexagonCanvas" to "60"
choose browse tool
end mouseUp

【问题讨论】:

  • 我在下面添加了绘制 6 个三角形的代码。看看吧。

标签: polygon livecode


【解决方案1】:

其中最难的部分是动态绘图。您当然可以编写一个程序来创建您的六边形饼图,但最好先画一次,然后简单地显示或隐藏它。

事物本身将是一组六个三角形,每个三角形都可以被寻址并具有其属性集、颜色、混合级别等。

如果您需要此小工具的多个副本,您可以克隆该组,并随意重命名它及其三角形组件。

一个警告。如果您确实以这种方式进行,您必须意识到,对于所有对象类中的单独组,关键字“last”是不稳定的。因此,您引用这个新组的能力(将最后一个组的名称设置为“yourNewGroupname”)以这种方式受到限制。有一个解决方法,使用模板组,但是,它工作得很好。我建议您阅读字典中“last”下的用户注释:

----“last”关键字在引用组时不稳定。因此,如果一个人创建了多个组,则引用“最后一个”组可能不会返回最后实际创建的组。使用“templateGroup”是一种解决方法,因为在创建新组时,例如,可以将 templateGroup 的名称设置为唯一的名称,并能够按名称找到最后一个组。此外,使用适当的脚本捕获“newGroup”消息可用于查找最后一个组。

希望这会有所帮助...

编辑。

您熟悉相关的属性吗?设置颜色的“backColor”,设置的“blendLevel”,嗯,blendLevel等?您是否曾经创建过类似三角形的图形并为其命名?您曾经对对象进行分组吗?

【讨论】:

  • 什么时候不稳定?
  • 很难依赖组的“最后一个”标识符。主要是在与其他组中的组一起工作时,或者当它们被引擎自动中继时(例如在使用“停止编辑 ”之后)。
  • 克雷格,非常感谢您的见解。到目前为止,我已经设法使用以下脚本绘制了一个六边形 - 在 mouseUp 选择线工具上将 dragSpeed 设置为 0 从 200,200 拖动到 300,267 从 300,267 拖动到 300,367 从 300,367 拖动到 200,433 从 200,433 拖动到 100,367 从 100,367 拖动到100,267 从 100,267 拖到 200,200 选择浏览工具 end mouseUp 但是我不知道如何命名新图形并将其保存为多边形,所以我可以做剩下的事情: -- 将 grc "HexagonCanvas" 的 backgroundColor 设置为蓝色 --将 grc "HexagonCanvas" 的混合级别设置为 "60"
  • (cont) 我希望接下来尝试一个类似的脚本来创建一个等边三角形 - 一个可以复制六次的三角形(围绕六边形的中心以 60 度增量)与边缘对齐六边形。
【解决方案2】:

非常勤奋。真的。

我的意思是使用多边形工具创建一个三角形图形。您可以将图形的边数设置为 3 并将其命名为“t1”。您可能需要对其进行调整以使其显示为等边形。现在这是一个完整的对象,可以复制五次。每个新对象都可以相应地命名(“t2”、“t3”等)并旋转,以便您可以将它们全部组装成一个漂亮的六边形。

现在您可以根据需要设置每个图形的 backColor 和 blendLevel。确保每个图形的“不透明”属性设置为“真”。如果将六个分组,您可以克隆新组,但您必须管理后代三角形的名称。

回到你之前的努力。您可以看到,即使您使用线条创建了一个漂亮的图形,也没有简单的方法来隔离各个三角形部分。多边形对象很好地解决了这个问题。

克雷格

【讨论】:

    【解决方案3】:

    参考

    http://en.wikipedia.org/wiki/Equilateral_triangle

    迭代步骤

    我采用了您找到的脚本,并进行了一些轻微的调整,它运行良好。有一个

    polygon polygonName, px, py, pColor
    

    鼠标向上脚本调用 4 次的脚本。它绘制了 4 个不同颜色的六边形。

    多边形脚本名不副实,因为它只能绘制六边形。

    然后是使用三角形脚本六次的 mouseup 脚本。

    on mouseup
       polygon "hex1", 100, 300, "green"
       polygon "hex2", 240, 300, "yellow"
       polygon "hex3", 380, 300, "red"
       polygon "hex4", 520, 300, "brown"
    end mouseup
    
    on polygon polygonName, px, py, pColor
       global tpoints
       local TL
       local TR
       local BL
       local BR
       local twidth
       local theight
       local twidthquart
       local theighthalf
    if exists(grc polygonName of this card) then delete grc polygonName
    lock screen
    create grc polygonName 
    
    set the loc of grc polygonName to px,py 
    
    set the opaque of grc polygonName to true
    -- resize the new grc
    get the rect of grc polygonName 
    add 80 to item 4 of it
    set the rect of grc polygonName to it
    put the topleft of grc polygonName into TL
    put the topright of grc polygonName into TR
    put the bottomleft of grc polygonName into BL
    put the bottomright of grc polygonName into BR
    put the width of grc polygonName into twidth
    put the height of grc polygonName into theight
    put trunc(twidth/4) into twidthquart
    put trunc(theight/2) into theighthalf
    #=========set the points for the "free" hexagon polygon==================
    put empty into tpoints
    put (item 1 of TL + twidthquart, item 2 of TL) into tpoints
    # for the first line of tpoints "put into"
    put Cr& (item 1 of TL, item 2 of TL + theighthalf) after tpoints
    put CR& (item 1 of BL + twidthquart, item 2 of BL) after tpoints
    put CR& (item 1 of BR - twidthquart, item 2 of BR) after tpoints
    put Cr& (item 1 of BR, item 2 of BR - theighthalf) after tpoints
    put CR& (item 1 of TR - twidthquart, item 2 of TR) after tpoints
    put CR& (item 1 of TL + twidthquart, item 2 of TL) after tpoints
    set the points of grc polygonName to tpoints
    set the style of grc polygonName to "polygon"
    set the backgroundColor of grc polygonName to pColor
    set the blendlevel of grc polygonName to "60"
    choose browse tool
    end polygon
    

    下一个迭代步骤

    继续各种方式都是可能的。

    一种易于遵循的方法是修改多边形脚本,使其适用于三角形而不是六边形。作为参数,它需要三角形的位置(遵循“时钟表盘约定或以度为单位给出扇区)

    坐标

                      (-r/2 ,h)                (r/2,h)
    
    
    
    
          -r/0                      0                        r/ 0
    
    
    
    
                      (-r/2 ,h)                (r/2,h)
    

    r = 半径 h = 高度

    h*h + (r/2)*(r/2) = r*r

    绘制六个三角形的代码

    `

    on mouseUp
       global tOffset
       put 140 into tOffset
       triangle "triangle1", -50,100, 0,0, -100, 0, "red"
       triangle "triangle2", 0,0,  -50, 100, 50,100, "green"
       triangle "triangle3", 50, 100,  100, 0, 0,0, "blue"
       triangle "triangle4", 0, 0,  100, 0, 50, -100, "yellow"
       triangle "triangle5", 0, 0,  50, -100, -50, -100, "orange"
       triangle "triangle6", 0, 0,  -50, -100, -100, 0, "purple"
    end mouseUp
    
    
    on triangle polygonName, pax, pay, pbx, pby, pcx, pcy, pColor
    
       global tpoints
       global tOffset
    
       if exists(grc polygonName of this card) then delete grc polygonName
    
       lock screen
    
       create grc polygonName 
       set the loc of grc polygonName to pax,pay 
       set the opaque of grc polygonName to true
    
       put empty into tpoints
    
       put (tOffset+pax, tOffset+pay) into tpoints
    
       put Cr& (tOffset+pbx, tOffset+pby) after tpoints
    
       put Cr& (tOffset+pcx, tOffset+pcy) after tpoints
    
       set the points of grc polygonName to tpoints
    
       set the style of grc polygonName to "polygon"
       set the backgroundColor of grc polygonName to pColor
       set the blendlevel of grc polygonName to "20"
    
       choose browse tool
    end triangle
    

    维基

    我将此作为社区 wiki 答案,以便人们可以轻松粘贴更新的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 2019-01-23
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多