← Back to topics
Topic

IntGrid - Help

A
Al3kSaNdaR
Can somebody help me how to calculate number of points. I used
For i:=0 To n Do Number:=Number + Trunc( ( m / n ) * i ) + 1 
for ( i = 0; i <= n; i++ ) { Number += (int) ( m / n * i ) + 1 } 
, But it's wrong for some cases. Why?
t
tgudlek
Try with Pick's theroem.
A
Al3kSaNdaR
Ok, thanks a lot. I'll give it a try. ;)
t
thewolf
Well, it's a matter of using some simple formulas:
Let's make z = number of int points on a diagonal = gcd(n, m).
Then, the number of points strictly inside is ((n-1)*(m-1)-z)/2, while the number of points on the vertices is n+m+z+1. Add those two and you have the answer ;).
A
Al3kSaNdaR
Thanks, I can't find number of point at the diagonal. ;)
p
pr0ton
Try looking for geometric meaning of GCD of two numbers. that may help
v
vasja
So what is the geometric meaning of GCD?
o
oduleodule
Vasja, try to solve this alone, search at internet, draw some cases
9 15
5 7
2 8
maybe then you understand this geometric meaning
v
vasja
OK i see through examples that the number of integer coordinates on the diagonal is GCD(n,m)+1 (including the end points) but i still don't understand why is that so?
Some tutorial on this maybe?
t
tgudlek
This is quite useful I think.
p
pr0ton
Geometric meaning of GCD

gcd(a,b) represents the number of integer points along 1,1 to a,b or b.a

so now in a rectangle from 0,0 to a,b
imagine it to be a two triangles joined

u have (1+a)*(1+b) points in the rectangle
subtract this by gcd(a,b) + 1 (1 for 0,0)

now divide this by 2 since there are two triangles and they are symmetric
this gives the points on the triangle but not on the diagonal
so add gcd(a,b) to this to get the answer

cheers, sorry for late reply