向量点积和向量叉积的对比,求解《1037. 有效的回旋镖》

向量积和数量积的区别

平面向量叉积的计算

向量点积和叉积的对比

如上图所示

例题

1037. 有效的回旋镖

给定一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点,如果这些点构成一个 回旋镖 则返回 true 。
回旋镖 定义为一组三个点,这些点 各不相同 且 不在一条直线上 。

答案

向量叉积

var isBoomerang = function(points) {  // (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)
  return (points[1][0] - points[0][0]) * (points[2][1] - points[0][1]) - (points[2][0] - points[0][0]) * (points[1][1] - points[0][1]) !== 0
};
function isBoomerang(points: number[][]): boolean { // 萨吕法则
  return (points[0][0] * points[1][1] + points[1][0] * points[2][1] + points[2][0] * points[0][1] - points[0][0] * points[2][1] - points[1][0] * points[0][1] - points[2][0] * points[1][1]) !== 0
};
func isBoomerang(points [][]int) bool {
  return (points[1][0] - points[0][0]) * (points[2][1] - points[0][1]) - (points[2][0] - points[0][0]) * (points[1][1] - points[0][1]) != 0
}
class Solution {
  function isBoomerang($points) {
    return ($points[0][0] * $points[1][1] + $points[1][0] * $points[2][1] + $points[2][0] * $points[0][1] - $points[0][0] * $points[2][1] - $points[1][0] * $points[0][1] - $points[2][0] * $points[1][1]) !== 0;
  }
}
class Solution {
  public boolean isBoomerang(int[][] points) {
    return (points[1][0] - points[0][0]) * (points[2][1] - points[0][1]) - (points[2][0] - points[0][0]) * (points[1][1] - points[0][1]) != 0;
  }
}
class Solution:
  def isBoomerang(self, points: List[List[int]]) -> bool:
    return (points[1][0] - points[0][0]) * (points[2][1] - points[0][1]) - (points[2][0] - points[0][0]) * (points[1][1] - points[0][1]) != 0
bool isBoomerang(int** points, int pointsSize, int* pointsColSize){
  return (points[1][0] - points[0][0]) * (points[2][1] - points[0][1]) - (points[2][0] - points[0][0]) * (points[1][1] - points[0][1]) != 0;
}
public class Solution {
  public bool IsBoomerang(int[][] points) {
    return (points[1][0] - points[0][0]) * (points[2][1] - points[0][1]) - (points[2][0] - points[0][0]) * (points[1][1] - points[0][1]) != 0;
  }
}
class Solution {
public:
  bool isBoomerang(vector<vector<int>>& points) {
    return (points[1][0] - points[0][0]) * (points[2][1] - points[0][1]) - (points[2][0] - points[0][0]) * (points[1][1] - points[0][1]) != 0;
  }
};