Maximum distinct lines passing through a single point in C

In computational geometry, finding the maximum number of distinct lines passing through a single point is a common problem. Given N lines defined by pairs of coordinates (x1,y1) and (x2,y2), we need to find how many lines with different slopes can pass through a single point.

Syntax

int maxDistinctLines(int n, int x1[], int y1[], int x2[], int y2[]);

Approach

We represent each line using the slope formula y = mx + c, where m is the slope calculated as āˆ’

  • For non-vertical lines: slope = (y2 - y1) / (x2 - x1)
  • For vertical lines: slope = infinity (represented as a large value)

Lines with the same slope are parallel, so we count distinct slopes to find the maximum number of non-overlapping lines.

Example 1: Basic Implementation

This example calculates slopes for given lines and counts distinct values āˆ’

#include <stdio.h>

int maxDistinctLines(int n, int x1[], int y1[], int x2[], int y2[]) {
    double slopes[100];
    int count = 0;
    
    for (int i = 0; i < n; i++) {
        double slope;
        
        // Handle vertical lines
        if (x1[i] == x2[i]) {
            slope = 999999.0; // Representing infinity
        } else {
            slope = (double)(y2[i] - y1[i]) / (x2[i] - x1[i]);
        }
        
        // Check if this slope already exists
        int found = 0;
        for (int j = 0; j < count; j++) {
            if (slopes[j] == slope) {
                found = 1;
                break;
            }
        }
        
        // Add new slope if not found
        if (!found) {
            slopes[count++] = slope;
        }
    }
    
    return count;
}

int main() {
    int n = 3;
    int x1[] = {1, 2, 0};
    int y1[] = {1, 2, 0};
    int x2[] = {3, 4, 2};
    int y2[] = {3, 4, 4};
    
    printf("Number of lines: %d
", n); printf("Maximum distinct lines: %d
", maxDistinctLines(n, x1, y1, x2, y2)); return 0; }
Number of lines: 3
Maximum distinct lines: 2

Example 2: With Vertical Lines

This example demonstrates handling of vertical lines āˆ’

#include <stdio.h>

int countDistinctSlopes(int n, int x1[], int y1[], int x2[], int y2[]) {
    double slopes[100];
    int distinctCount = 0;
    
    for (int i = 0; i < n; i++) {
        double currentSlope;
        
        if (x2[i] - x1[i] == 0) {
            currentSlope = 1000000.0; // Vertical line
        } else {
            currentSlope = (double)(y2[i] - y1[i]) / (x2[i] - x1[i]);
        }
        
        // Check for duplicate slopes
        int isDuplicate = 0;
        for (int j = 0; j < distinctCount; j++) {
            if (slopes[j] == currentSlope) {
                isDuplicate = 1;
                break;
            }
        }
        
        if (!isDuplicate) {
            slopes[distinctCount] = currentSlope;
            distinctCount++;
        }
    }
    
    return distinctCount;
}

int main() {
    int n = 4;
    int x1[] = {1, 2, 3, 5};
    int y1[] = {1, 2, 5, 7};
    int x2[] = {3, 4, 3, 5};  // Note: third line is vertical
    int y2[] = {3, 4, 8, 10}; // fourth line is also vertical
    
    int result = countDistinctSlopes(n, x1, y1, x2, y2);
    printf("Total lines: %d
", n); printf("Maximum distinct lines: %d
", result); return 0; }
Total lines: 4
Maximum distinct lines: 3

Key Points

  • Slope calculation: Use floating-point division to handle fractional slopes correctly
  • Vertical lines: Assign a special value (like infinity) when x1 = x2
  • Duplicate detection: Compare slopes to identify parallel lines
  • Time complexity: O(n²) for checking duplicates, can be optimized with sorting

Conclusion

The maximum distinct lines problem is solved by calculating slopes and counting unique values. Proper handling of vertical lines and floating-point comparison ensures accurate results for all geometric configurations.

Updated on: 2026-03-15T13:04:41+05:30

294 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements