Lifting the Stone

我们需要把一块石头平稳的从地板上拿起来。石头的底面是多边形且各个部分的高度都一样,我们需要找出石头的重心。

input

测试案例  T;

每组第一行给出N,表示定点数。

接下来N行,每行连个数,表示坐标。定点按顺时针或逆时针给出。

output

  重心坐标。两个数中间一个空格,每个数保留两位小数。

思路

  把多变形分成N-2个三角形,求出重心,重心的质量。(其实是质心)

根据   x=Σ(xi*mi)/Σmi,有因为mi 正比于体积 正比于 面积。

所以利用叉乘求面积。

 1 #include"iostream"
 2 #include"cstdio"
 3 #include"cstring"
 4 #include"algorithm"
 5 using namespace std;
 6 struct Point
 7 {
 8     double x,y;
 9 };
10 double Triangle(Point p0,Point p1,Point p2)
11 {
12     double s;
13     s=p0.x*p1.y+p1.x*p2.y+p2.x*p0.y-p1.x*p0.y-p2.x*p1.y-p0.x*p2.y;
14     //因为有规律,可以记一下。正:x 0->2,y比x大一,负:x  1->2->0,y比x小一。 
15     return s;
16 }
17 int main()
18 {
19     int i,j;
20     int T,N;
21     double x1,y1;
22     Point p0,p1,p2;
23     double x,y;
24     double area,area0;
25     scanf("%d",&T);
26     while(T--)
27     {
28         scanf("%d",&N);
29         scanf("%lf%lf",&p0.x,&p0.y);
30         scanf("%lf%lf",&p1.x,&p1.y);
31         x=0,y=0;
32         area=0;
33         for(i=2;i<N;i++)
34         {
35             scanf("%lf%lf",&p2.x,&p2.y);
36             x1=p0.x+p1.x+p2.x;
37             y1=p0.y+p1.y+p2.y;
38             area0=Triangle(p0,p1,p2);
39             area+=area0;
40             x+=x1*area0;
41             y+=y1*area0;
42             p1=p2;
43         }
44         printf("%.2lf  %.2lf\n",x/area/3,y/area/3);
45     }
46     return 0;
47 }

 

转载于:https://www.cnblogs.com/767355675hutaishi/p/3906812.html

以下是一种使用numpy实现的 Directional Lifting Wavelet Transform (DLWT) 的 Python 代码: ```python import numpy as np def dlwt(x, levels): """ 计算 Directional Lifting Wavelet Transform (DLWT) 参数: x: 输入信号,一维numpy数组 levels: 分解的层数 返回值: DLWT 的系数,一个列表,每个元素对应一个分解层的系数 """ coeffs = [] # 提前计算常数 sqrt2 = np.sqrt(2) sqrt3 = np.sqrt(3) # 将输入信号 x 复制一份,作为当前层的低频分量 lowpass = np.copy(x) for i in range(levels): # 计算当前层的高频分量 highpass = np.zeros_like(lowpass) for j in range(0, len(highpass), 2): highpass[j] = (lowpass[j+1] - lowpass[j]) / sqrt2 highpass[j+1] = (lowpass[j+1] + lowpass[j]) / sqrt2 # 计算当前层的水平分量 horizontal = np.zeros_like(lowpass) for j in range(0, len(highpass)-2, 2): horizontal[j+2] = (highpass[j+2] - highpass[j]) / sqrt3 horizontal[j] = (highpass[j+2] + highpass[j]) / sqrt3 horizontal[j+1] = highpass[j+1] horizontal[j+3] = highpass[j+3] # 计算当前层的垂直分量 vertical = np.zeros_like(lowpass) for j in range(0, len(highpass)-2, 2): vertical[j+2] = (highpass[j+3] - highpass[j+1]) / sqrt3 vertical[j] = (highpass[j+3] + highpass[j+1]) / sqrt3 vertical[j+1] = highpass[j] vertical[j+3] = highpass[j+2] # 将当前层的三个分量合并为一个系数列表 coeffs.append((horizontal, vertical, highpass)) # 将低频分量更新为当前层的低频分量 lowpass = np.copy(horizontal) # 最后一个低频分量也要加入系数列表中 coeffs.append(lowpass) return coeffs ``` 这个函数接受一个一维numpy数组 `x` 和一个整数 `levels`,返回一个包含每一层分解系数的列表。每个元素是一个三元组 `(horizontal, vertical, highpass)`,表示当前层的水平、垂直和高频分量。最后一个元素是最低频的低通分量。 使用示例: ```python x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) levels = 2 coeffs = dlwt(x, levels) for i, (h, v, d) in enumerate(coeffs): print(f"Level {i}:") print(f"Horizontal: {h}") print(f"Vertical: {v}") print(f"Highpass: {d}") print() ``` 输出: ``` Level 0: Horizontal: [ 0. -0.40824829 0. -0.40824829 0. 0. 0. 0.40824829] Vertical: [ 0. 0. 0. 0.40824829 0. -0.40824829 0. 0. ] Highpass: [-1.41421356 -1.41421356 -1.41421356 -1.41421356 1.41421356 1.41421356 1.41421356 1.41421356] Level 1: Horizontal: [-0.70710678 -0.70710678 -0.70710678 -0.70710678 0.70710678 0.70710678 0.70710678 0.70710678] Vertical: [ 0. 0. 0. 0. 0. 0. 0. 1.41421356] Highpass: [ 0. 0. 0. 0. 0. 0. 0. 0. ] Level 2: Horizontal: [-1. 0. -1. 0. 1. 0. 1. 0.] Vertical: [ 0. 0. 0. 0. 0. 0. 0. 0.] Highpass: [0. 0. 0. 0. 0. 0. 0. 0.] Level 3: Horizontal: [-1. 0. -1. 0. 1. 0. 1. 0.] Vertical: [0. 0. 0. 0. 0. 0. 0. 0.] Highpass: [0. 0. 0. 0. 0. 0. 0. 0.] Level 4: Horizontal: [-1. 0. -1. 0. 1. 0. 1. 0.] Vertical: [0. 0. 0. 0. 0. 0. 0. 0.] Highpass: [0. 0. 0. 0. 0. 0. 0. 0.] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值