2017-09-14 54 views
1

我正在製作一個腳本來確定一個形狀是否已被扭曲或縮放。它首先列出一個形狀中所有線的長度,如下所示。AppleScript來確定一個形狀是否已被扭曲或縮放

set noofsides to text returned of (display dialog "Enter number of sides:" default answer "") 

set sidevalues to {} 
set repeatnumber to 0 
repeat noofsides times 
    set repeatnumber to repeatnumber + 1 
    set currentsidevalue to text returned of (display dialog "Enter length of line " & repeatnumber & ":" default answer "") 
    set the end of sidevalues to currentsidevalue 
end repeat 

然後它爲第二個編輯形狀做同樣的事情。這給了我兩個不同變量的列表。爲了確定這兩個形狀是否相似,每條'之前'行與每條'之後'行之間的分隔必須相同。例如,對於一個三角形:

firstline1/secondline1 = firstline2/secondline2 = firstline3/secondline3 

有沒有什麼辦法可以快速做到這一點,而無需執行以下操作:

try 
    set primevariable1 to first item of primesidevalues 
    set primevariable2 to second item of primesidevalues 
    set primevariable3 to third item of primesidevalues 
    -- ... 
end try 

try 
    set regularvariable1 to first item of sidevalues 
    set regularvariable2 to second item of sidevalues 
    set regularvariable3 to third item of sidevalues 
    -- ... 
end try 

try 
    variable4 
on error 
    set variable4 to "" 
end try 
if (regularvariable1/primevariable1) = (regularvariable2/primevariable2) and (regularvariable3/primevariable3) = (regularvariable1/primevariable1) and (regularvariable3/primevariable3) = (regularvariable2/primevariable2) and variable4 = "" then 
    display dialog "Shape is similar" 
end if 

這只是一個三面形狀。如果我想用5或6個方面做點什麼,這會變得越來越長。也許像是如果將第一列中的每一個數除以第二列中的每一個數都相等,那麼形狀是相似的?誰能幫忙?

+0

在蘋果腳本中是否存在循環和索引變量(列表/數組)? – MBo

+0

是的,但我不太確定這可以在這裏實現。 –

+0

似乎'設置sidevalues {}'表示'sidevalues'是一些索引數據結構 – MBo

回答

0

獲取的值(在第一個列表中的第一項/在第二個列表的第一項),使用循環比較的其他物品的價值,就像這樣:

set sidevalues to my getSidesValue("Enter number of sides for the first shape:") 
set primesidevalues to my getSidesValue("Enter number of sides for the second shape:") 
set tc to count sidevalues 
if tc = (count primesidevalues) then -- the number of items in the lists is the same 
    set isSimilar to true 
    set thisVal to (item 1 of sidevalues)/(item 1 of primesidevalues) -- Get the value of the first item in the lists 
    repeat with i from 2 to tc -- loop to compare the value of the others items 
     if (item i of sidevalues)/(item i of primesidevalues) is not thisVal then -- not the same value 
      set isSimilar to false 
      exit repeat -- no need to continue 
     end if 
    end repeat 
else 
    set isSimilar to false 
end if 
isSimilar 


on getSidesValue(t) 
    set noofsides to text returned of (display dialog t default answer "") 
    set l to {} 
    repeat with i from 1 to noofsides 
     set currentsidevalue to text returned of (display dialog "Enter length of line " & i & ":" default answer "") 
     set the end of l to currentsidevalue 
    end repeat 
    return l 
end getSidesValue