Fundamentals 6 min read

Compute a Point's Mirror Across a Line with Java – Step‑by‑Step Guide

The article blends a personal dilemma about relocating for a partner with a detailed guide on computing a point’s mirror across a line using geometry formulas and Java code, offering step‑by‑step calculations, common pitfalls, and a complete implementation.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Compute a Point's Mirror Across a Line with Java – Step‑by‑Step Guide

A net‑izen shares a personal story: his wife lives in Chengdu while he works in Guangzhou, and he is considering quitting a senior manager position at a tech company to move, sacrificing a 200,000 CNY annual salary. The post presents two viewpoints—practical concerns about the loss versus the long‑term value of being together—and advises a detailed cost‑benefit analysis of living expenses, career prospects, and relationship goals.

The technical portion poses a classic computational‑geometry problem: given a line (in general form Ax + By + C = 0) and a point P(x0, y0), find the mirror point P'(x', y') reflected across the line.

Solution Steps

Compute the line coefficients from two points (x1, y1) and (x2, y2):

A = y2 - y1
B = x1 - x2
C = x2*y1 - x1*y2

Find the foot of the perpendicular H(hx, hy) from P to the line using:

d = (A*x0 + B*y0 + C) / (A*A + B*B)
hx = x0 - A*d
hy = y0 - B*d

Compute the mirror point by symmetry:

x' = 2*hx - x0
y' = 2*hy - y0

Common mistakes include writing A^2 + B^2 as A^2 + B, which sends the result far off. The output is typically required with six decimal places.

Reference Java Implementation

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        // line defined by two points
        double x1 = sc.nextDouble();
        double y1 = sc.nextDouble();
        double x2 = sc.nextDouble();
        double y2 = sc.nextDouble();
        // point to be mirrored
        double x0 = sc.nextDouble();
        double y0 = sc.nextDouble();
        // line coefficients Ax + By + C = 0
        double A = y2 - y1;
        double B = x1 - x2;
        double C = x2 * y1 - x1 * y2;
        double denom = A * A + B * B;
        if (denom == 0) {
            // degenerate line – output original point
            System.out.printf("%.6f %.6f%n", x0, y0);
            return;
        }
        double d = (A * x0 + B * y0 + C) / denom;
        double hx = x0 - A * d;
        double hy = y0 - B * d;
        double xr = 2 * hx - x0;
        double yr = 2 * hy - y0;
        System.out.printf("%.6f %.6f%n", xr, yr);
    }
}

The algorithm reduces the geometric reflection to a projection and a midpoint calculation, avoiding case‑by‑case handling of vertical, horizontal, or undefined slopes. It can be extended to batch‑process multiple points by looping the formula.

algorithmComputational GeometrygeometryMirror Point
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.