aboutsummaryrefslogtreecommitdiff
path: root/challenge-093/paulo-custodio/basic/ch-1.bas
blob: 2d11f3de56b46ddf48186eeab646868393e2e630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
' Challenge 093
'
' TASK #1 › Max Points
' Submitted by: Mohammad S Anwar
' You are given set of co-ordinates @N.
'
' Write a script to count maximum points on a straight line when given co-ordinates plotted on 2-d plane.
'
' Example 1:
' |
' |     x
' |   x
' | x
' + _ _ _ _
'
' Input: (1,1), (2,2), (3,3)
' Output: 3
' Example 2:
' |
' |
' | x       x
' |   x
' | x   x
' + _ _ _ _ _
'
' Input: (1,1), (2,2), (3,1), (1,3), (5,3)
' Output: 3

Type PointType
    x as Integer
    y as Integer
End Type

Dim Shared points() as PointType

' read points from command line
Sub read_points()
    Dim i as Integer
    i = 0
    Do While Command(1+2*i) <> ""
        Redim Preserve points(i) as PointType
        points(i).x = Val(Command(1+2*i))
        points(i).y = Val(Command(1+2*i+1))
        i = i+1
    Loop
End Sub

' check if three points are in a line
Function three_in_line(i as PointType, j as PointType, k as PointType) as Boolean
    Dim dxc as Integer, dyc as Integer, dxl as Integer, dyl as Integer, cross as Integer
    dxc = k.x - i.x: dyc = k.y - i.y
    dxl = j.x - i.x: dyl = j.y - i.y
    cross = dxc * dyl - dyc * dxl
    three_in_line = (cross = 0)
End Function

' count total points through given two points
Function count_in_line(i as PointType, j as PointType) as Integer
    Dim npoints as Integer
    Dim k as Integer

    npoints = 0
    For k=LBound(points) to UBound(points)
        If three_in_line(i, j, points(k)) Then
            npoints = npoints+1
        End If
    Next k
    count_in_line = npoints
End Function

' count max points in line
Function count_max_in_line() as Integer
    Dim max_npoints as Integer, npoints as Integer
    Dim i as Integer, j as Integer

    max_npoints = 0
    For i=LBound(points) to UBound(points)-1
        For j=i+1 to UBound(points)
            npoints = count_in_line(points(i), points(j))
            If npoints > max_npoints Then
                max_npoints = npoints
            End If
        Next j
    Next i
    count_max_in_line = max_npoints
End Function


' main
read_points
print count_max_in_line()