Triangle ~~ 叉积

本文探讨了如何在三角形中找到一条线段,使其两端分别位于三角形边界上,并将三角形分为面积相等的两部分。文章提供了一种算法,首先确定给定点在三角形哪条边上,然后计算另一端点的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目超链接

One day, ABC and DD found a triangular wheat field, and they decide to cut it into two pieces with the same area with a segment. Because of various reasons, one of the endpoints of the division line is fixed at (px,py). Now you are asked to find the other endpoint of the segment.
If the given endpoint does not lie on the boundary of the wheat field, the problem should be regarded as invalid. The other endpoint required should also locate on the boundary.

Input

The input provides several test cases and the first line of the input contains a single integer T (1≤T≤1000000) indicating the number of cases.
For each test case, eight integers x1​,y1​,x2​,y2​,x3​,y3​,px​ and py are given in a line, where (x1,y1),(x2,y2) and (x3,y3)describe the coordinates of vertices for the given triangle wheat field which are guaranteed to be not colinear, and (px,py) is the given endpont of the segment. All coordinates are integers in the range [0,100000].

Output

For each test case, output the coordinate of the other endpoint of the division segment, or output the number -1 if the problem, in this case, is invalid.
Formally, if your answer is aaa and the jury’s answer is bbb, then your answer will be considered correct if and only if 在这里插入图片描述

本题答案不唯一,符合要求的答案均正确
样例输入

2
0 0 1 1 1 0 1 0
0 0 1 1 1 0 2 0

样例输出

0.500000000000 0.500000000000
-1

题意:给你三角形的三个顶点,再给你一个点,若该点在三角形上,则再在这个三角形上找一点,连接着两个点使得三角形被分成面积相等的两半。否则输出-1。
思路:我们只讨论询问点在三角形上的情况,首先确定询问点在哪条边上,再确定答案点在哪条边上。(都是通过叉积判断的),具体实现看代码:

#include<bits/stdc++.h>
#define eps 1e-8
#define zero(x) (!(((x)>0?(x):-(x))<eps)) // x = 0 返回 false

using namespace std;
struct point {double x, y; point(){} point(double x, double y): x(x), y(y){}};

double cross(point a, point b, point c) {
  return fabs((b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x));
}

void solve(point a, point b, point c, point d, double S) {
  double k = S / cross(b, d, c); // S = cross(kcb * db)
  printf("%.12f %.12f\n",(c.x-b.x)*k + b.x, (c.y-b.y)*k + b.y);
}

int main() {
  int T; scanf("%d", &T);
  while(T --) {
    point a, b, c, d;
    scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y);
    double S=cross(a,b,c),s1=cross(d,a,b),s2=cross(d,b,c),s3=cross(d,c,a);
    if ((zero(S-s1-s2-s3))||zero(s1)&&zero(s2)&&zero(s3)) printf("-1\n");
    else if (!zero(s1)) {if (s2>s3) solve(a,b,c,d,S/2); else solve(b,a,c,d,S/2);}
    else if (!zero(s2)) {if (s1>s3) solve(c,b,a,d,S/2); else solve(b,c,a,d,S/2);}
    else if (!zero(s3)) {if (s1>s2) solve(c,a,b,d,S/2); else solve(a,c,b,d,S/2);}
  }
  return 0;
} 
"sgmediation.zip" 是一个包含 UCLA(加利福尼亚大学洛杉矶分校)开发的 sgmediation 插件的压缩包。该插件专为统计分析软件 Stata 设计,用于进行中介效应分析。在社会科学、心理学、市场营销等领域,中介效应分析是一种关键的统计方法,它帮助研究人员探究变量之间的因果关系,尤其是中间变量如何影响因变量与自变量之间的关系。Stata 是一款广泛使用的统计分析软件,具备众多命令和用户编写的程序来拓展其功能,sgmediation 插件便是其中之一。它能让用户在 Stata 中轻松开展中介效应分析,无需编写复杂代码。 下载并解压 "sgmediation.zip" 后,需将解压得到的 "sgmediation" 文件移至 Stata 的 ado 目录结构中。ado(ado 目录并非“adolescent data organization”缩写,而是 Stata 的自定义命令存放目录)目录是 Stata 存放自定义命令的地方,应将文件放置于 "ado\base\s" 子目录下。这样,Stata 启动时会自动加载该目录下的所有 ado 文件,使 "sgmediation" 命令在 Stata 命令行中可用。 使用 sgmediation 插件的步骤如下:1. 安装插件:将解压后的 "sgmediation" 文件放入 Stata 的 ado 目录。如果 Stata 安装路径是 C:\Program Files\Stata\ado\base,则需将文件复制到 C:\Program Files\Stata\ado\base\s。2. 启动 Stata:打开 Stata,确保软件已更新至最新版本,以便识别新添加的 ado 文件。3. 加载插件:启动 Stata 后,在命令行输入 ado update sgmediation,以确保插件已加载并更新至最新版本。4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值