943,915 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 861
  • Perl RSS
Nov 12th, 2007
0

help.....

Expand Post »
Please help me with this. I am counting how many triangles I can find in the attached image.
Pl. see the attachment. I do not know how to do it the right way and please fix my code. I tried as best as I can to gather all points that make a line. Please show me a simpler way.
Thanks
----------------------


#!/usr/bin/perl

use strict;
# I listed as many points as I could find that make up a line
my @points = ([8,0], [8,22,57,120,211,330,477,642,1], [8,21,56,119,210,329,476,651,28,69,138,235,360,513,694,2], [8,20,55,118,209,328,475,650,27,68,137,234,359,512,693,33,79,153,271,405,567,757,3], [8,19,54,117,208,327,474,649,26,67,136,233,358,511,692,32,78,152,270,404,566,756,40,93,174,283,420,585,778,4], [8,18,53,116,207,326,473,648,25,66,135,232,357,510,691,31,77,151,269,403,565,755,39,92,173,282,419,584,777,42,97,180,291,430,597,792,5], [8,17,52,115,206,325,472,647,24,65,134,231,356,509,690,30,76,150,268,402,564,754,38,91,172,281,418,583,776,41,96,179,290,429,596,791,43,99,183,295,435,603,799,6], [8,16,51,114,205,324,471,646,23,64,133,230,355,508,689,29,75,149,267,401,563,753,37,90,171,280,417,582,775,40,95,178,289,428,595,790,42,98,182,294,434,602,798,9,10,11,12,13,14,15,7],
[9,16,17,18,19,20,21,22,0], [9,23,24,25,26,27,28,1], [9,29,30,31,32,33,2], [9,34,35,36,37,3], [9,38,39,40,4], [9,41,42,5], [9,43,6], [9,7],
[10,44,45,46,47,48,49,50,51,52,53,54,55,56,57,0], [10,58,59,60,61,62,63,64,65,66,67,68,69,1], [10,70,71,72,73,74,75,76,77,78,79,2], [10,80,81,82,83,84,85,86,87,3], [10,88,89,90,91,92,93,4], [10,94,95,96,97,5], [10,98,99,6], [10,7],
[11,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,0], [10,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,1], [11,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,2], [11,154,155,156,157,158,159,160,161,162,163,164,165,3], [11,166,167,168,169,170,171,172,173,174,4], [11,175,176,177,178,179,180,5], [11,181,182,183,6], [11,7],
[12,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,0], [12,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,27,228,229,230,231,232,233,234,235,1], [12,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,354,255,2], [12,256,257,258,259,260,261,262,23,264,265,266,267,268,269,270,271,3], [12,272,273,274,275,276,277,278,279,280,281,282,283,4], [12,284,285,286,287,288,289,290,291,5], [12,292,293,294,295,6], [12,7],
[13,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,32,328,329,330,0], [13,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,58,359,360,1], [13,361,362,363,364,365,366,367,368,369,370,371,37,373,374,375,376,377,378,379,380,381,382,383,384,385,2], [13,386..405,3], [13,406..420,4], [13,421,422,423,424,425,426,427,428,429,430,5], [13,431,432,433,434,435,6], [13,7],
[14,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,0], [14,478..513,1], [14,514..543,2], [14,544..567,3], [14,568,569,580,571,572,573,574,575,576,577,578,579,580,581,582,583,584,585,4], [14,586,587,588,589,590,591,592,593,594,595,596,597,5], [14,598,599,600,601,602,603,6], [14,7],
[15,604..652,0], [15,653..694,1], [15,695..729,2], [15,730..757,3], [15,758..778,4], [15,779,780,781,782,783,784,785,786,787,788,789,790,791,792,5], [15,793,794,795,796,797,798,799,6], [15,7]
);
my %triangles;
my @line;
my $lineNo=1;

# labelled thelines
for my $lineref (@points) {
for(my $i=0; $i<=$#{$lineref}; $i++) {
for(my $j=0; $j<=$#{$lineref}; $j++) {
next if $i==$j;
$line[$lineref->[$i]][$lineref->[$j]]=$lineNo;
$line[$lineref->[$j]][$lineref->[$i]]=$lineNo;
}
}
$lineNo++;
}

for(my $i=0; $i<800; $i++) {
for(my $j=0; $j<800; $j++) {
for(my $k=0; $k<800; $k++) {
# 3 points form a triangle if
# 1) not all on 1 straight line
# 2) each pair has common line
next if $i==$j || $i==$k || $j==$k;
next if(!$line[$i][$j] || !$line[$i][$k]
|| !$line[$j][$k]);
next if ($line[$i][$j]==$line[$i][$k]);
my @arr = sort ($i,$j,$k);
$triangles{$arr[0],$arr[1],$arr[2]}=1;
}
}
}
printf "No. of triangles %d\n", scalar keys %triangles;
Attached Thumbnails
Click image for larger version

Name:	Untitled.jpg
Views:	10
Size:	339.1 KB
ID:	4433  
Reputation Points: 10
Solved Threads: 0
Newbie Poster
geethab is offline Offline
1 posts
since Nov 2007
Nov 12th, 2007
0

Re: help.....

is this school work?
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Dec 14th, 2007
0

Re: help.....

What is the result you are getting so far using the perl script?
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
chrisranjana is offline Offline
82 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: word count on a file (looked online but didn't help much)
Next Thread in Perl Forum Timeline: Parse and mail log files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC