【leetcode】【hard】【每日一题】面试题 16.03. Intersection LCCI

面试题 16.03. Intersection LCCI

Given two straight line segments (represented as a start point and an end point), compute the point of intersection, if any. If there's no intersection, return an empty array.

The absolute error should not exceed 10^-6. If there are more than one intersections, return the one with smallest X axis value. If there are more than one intersections that have same X axis value, return the one with smallest Y axis value.

Example 1:

Input: 
line1 = {0, 0}, {1, 0}
line2 = {1, 1}, {0, -1}
Output:  {0.5, 0}

Example 2:

Input: 
line1 = {0, 0}, {3, 3}
line2 = {1, 1}, {2, 2}
Output:  {1, 1}

Example 3:

Input: 
line1 = {0, 0}, {1, 1}
line2 = {1, 0}, {2, 1}
Output:  {}  (no intersection)

Note:

  • The absolute value of coordinate value will not exceed 2^7.
  • All coordinates are valid 2D coordinates.

题目链接:https://leetcode-cn.com/problems/intersection-lcci/

 

思路

这道数学题,计算真恶心。。就算是高考手算也要算好久。

思路只能想到用斜线计算交点,然后判断是否在线段内。

官方还给出了一种用向量叉乘的方法:https://leetcode-cn.com/problems/intersection-lcci/solution/jiao-dian-by-leetcode-solution/

先记下这道题但不编程,等刷题后期再做这种数学题。

你可能感兴趣的:(todo,code,c++)