看板 C_Sharp 關於我們 聯絡資訊
※ 引述《awd (672012)》之銘言: : 我開了一個class的cs檔 : 主要是寫影像中的物件連通 : 目前是寫了一個副函式 : 會計算出影像中每個長方形的四點座標(x,y) : 我的問題是 : 如果我這副函式算出這張圖有N個長方形 : 想回傳每個長方形的四點座標給另一個cs檔的主程式接收使用 : 要如何寫這樣的回傳? : 譬如副函式算出了此圖有3個長方形 : 長方形1的四點座標分別是(10,13) (10,70) (20,13) (20,70) : 長方形2的四點座標分別是(17,50) (17,62) (53,50) (53,62) : 長方形3的四點座標分別是(0,21) (0,49) (30,21) (30,49) : 我想要以上資訊都從此副函式輸出給其他類別或主程式使用, : 請問有比較適合c#的寫法嗎??(c#的結構很難跟指標結合..) : 這問題我困擾好久 : 目前只能在此副函式中用區域變數存著,想不到怎麼丟出資訊 : 請高手提供些解法~ : 感謝~ public struct rectangleInfo { public Point topLeft { get; set; } public Point topRight { get; set; } public Point bottomLeft { get; set; } public Point bottomRight { get; set; } } 你的副程式 // 假裝是這樣 public ArrayList calculateRectangle() { ArrayList resultArray = new ArrayList; rectangleInfo rInfo = null; // 計算座標 // 建立 rInfo instance rInfo = new rectangleInfo(); rInfo.topLeft = new Point(1,1); rInfo.topRight = new Point(1,1); rInfo.bottomLeft = new Point(1,1); rInfo.bottomRight = new Point(1,1); // 將得到的座標加進 ArrayList resultArray.Add(rInfo); // 加入第二個矩形資料 rInfo = new rectangleInfo(); ... ... ... // 最後回傳這個 ArrayList return resultArray; } // 主程式或其他要接收此資料的類別 public static void main() { ArrayList rectInfo = calculateRectangle(); foreach (rectangleInfo rInfo in rectInfo) { // 取得矩形左上角的座標 rInfo.topLeft } } /* foreach 的敘述可能有錯,我每次都跟 php 搞混ˊˋ,如果錯了可能就是 foreach as。 也許可以為 struct rectangleInfo 加上一個 method 來一次設定所有點的值 例如: public void setAllPoint(Point tl, Point tr, Point bl, Point br) { this.topLeft = tl; this.topRight = tr; // ... 以此類推 } 這樣似乎不用寫這麼多行 XD */ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 203.145.202.66
blestadsl:如果是我~我會用List<rectangleInfo> 01/02 15:51
chrisQQ:yes 樓上的方法比較好~ 01/02 18:53
awd:感恩兩位想法~現在試寫中~ 01/02 18:55