Skip to content

Fix point-to-line distance calculation for vertical lines#4

Closed
Copilot wants to merge 2 commits intomainfrom
copilot/nice-tyrannosaurus
Closed

Fix point-to-line distance calculation for vertical lines#4
Copilot wants to merge 2 commits intomainfrom
copilot/nice-tyrannosaurus

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 13, 2025

The existing implementation used slope-intercept form, causing division by zero for vertical lines (dx=0), resulting in NaN output.

Changes

  • Replaced slope-intercept method with cross product formula: distance = |(B-A) × (A-P)| / |B-A|
  • Removes TODO comment and completes implementation

Before/After

// Before: fails on vertical lines
double dx = L.point_A.x - L.point_B.x;
double dy = L.point_A.y - L.point_B.y;
double k = dy / dx;  // division by zero if dx=0
double b1 = L.point_A.y - k*L.point_A.x;
double distance = fabs(k*P.x - P.y + b1) / sqrt(k*k + 1);

// After: handles all cases
double dx = L.point_B.x - L.point_A.x;
double dy = L.point_B.y - L.point_A.y;
double crossProduct = fabs(dx * (L.point_A.y - P.y) - (L.point_A.x - P.x) * dy);
double lineLength = sqrt(dx * dx + dy * dy);
double distance = crossProduct / lineLength;

Test case echo "1 0 0 0 0 1" | ./program now outputs 1.00 instead of nan.

Original prompt

处理 TODO: 计算点P到直线L的距离 (来自 niuke/practice/noob/noob72 点到直线距离/1.cpp)

Created from VS Code via the GitHub Pull Request extension.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@tkzzzzzz6 tkzzzzzz6 marked this pull request as ready for review December 13, 2025 14:06
Copilot AI review requested due to automatic review settings December 13, 2025 14:06
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This is a work-in-progress pull request that implements the calculation of the distance from a point to a line in a C++ competitive programming solution. The PR addresses a TODO comment by adding the mathematical implementation for this geometric calculation.

Key Changes (Inferred):

  • Implementation of the getDistance function to calculate point-to-line distance
  • Uses slope-intercept form approach with the formula |kx - y + b| / sqrt(k² + 1)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: tkzzzzzz6 <147895238+tkzzzzzz6@users.noreply.github.com>
Copilot AI changed the title [WIP] Add calculation for distance from point to line Fix point-to-line distance calculation for vertical lines Dec 13, 2025
Copilot AI requested a review from tkzzzzzz6 December 13, 2025 14:12
Copy link
Copy Markdown
Owner

@tkzzzzzz6 tkzzzzzz6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good

Comment thread niuke/practice/noob/noob72 点到直线距离/1.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants