vector.xy()
时间: 2024-01-06 13:25:39 浏览: 36
根据提供的引用内容,vector是C++标准模板库(STL)中的一个容器,用于存储一组元素。下面是关于vector的基本使用方法:
1. 初始化vector:
```cpp
vector<int> v; // 初始化一个空的vector
vector<int> v(10, 1); // 初始化一个包含10个值为1的元素的vector
vector<int> v(v1); // 将另一个vector v1 的元素拷贝给v
vector<int> v(v1.begin(), v2.end()); // 将v1中从第一个元素到最后一个元素(不包括最后一个元素)的范围拷贝给v
```
2. 插入元素到vector中:
```cpp
v.push_back(5); // 在vector的末尾插入一个元素5
v.insert(v.begin() + 2, 10); // 在vector的第3个位置(索引为2)插入一个元素10
```
3. 遍历vector中的元素:
```cpp
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " "; // 输出vector中的每个元素
}
```
4. 删除vector中的元素:
```cpp
v.pop_back(); // 删除vector的最后一个元素
v.erase(v.begin() + 2); // 删除vector的第3个元素(索引为2)
```
相关问题
vector resize方法
vector的resize方法是用来改变vector的大小的。如果参数的数值比vector的当前大小大,那么vector会用0来填充新增的元素;如果参数的数值比vector的当前大小小,那么在设置的大小和当前大小之间的元素会被删除。比较两个vector是否相等时,会比较它们的大小和对应位置上的元素是否相等。
具体来说,resize方法有两个参数:_Newsize和_Val。_Newsize用来设置vector的新大小,_Val是可选的,用来为新增的元素赋值。注意,resize方法并不会改变之前数组的值,只会改变数组的长度和新增元素的值。
下面是一个示例代码,展示了resize方法的使用:
```cpp
#include <iostream>
#include <vector>
using namespace std;
struct squareXY {
int x;
int y;
};
int main() {
squareXY rexy = {1, 2};
squareXY xy = {3, 4};
vector<squareXY> s(10, xy);
cout << "resize之前的数组:" << endl;
for (int i = 0; i < s.size(); i++) {
cout << s[i].x << "------" << s[i].y << endl;
}
s.resize(20, rexy);
cout << "resize之后的数组:" << endl;
for (int i = 0; i < s.size(); i++) {
cout << i << ":" << s[i].x << "------" << s[i].y << endl;
}
return 0;
}
```
运行以上代码,可以看到resize方法改变了vector的大小,并根据需要进行元素的填充或删除。
const float HEAT_MAX = 10.; const float PointRadius = .42; // test data const int PointCount = 18; const vec3 Points[] = vec3[]( vec3(0., 0., 10.), vec3(.2, .6, 5.), vec3(.25, .7, 8.), vec3(.33, .9, 5.), vec3(.35, .8, 6.), vec3(.1, .1, 6.), vec3(-.45, .8, 4.), vec3(-.2, -.6, 5.), vec3(-.25, -.7, 8.), vec3(-.33, -.9, 8.), vec3(.35, -.45, 10.), vec3(-.1, -.8, 10.), vec3(.33, -.3, 5.), vec3(-.35, .75, 6.), vec3(.6, .4, 10.), vec3(-.4, -.8, 4.), vec3(.7, -.3, 6.), vec3(.3, -.8, 8.) ); vec3 gradient(float w, vec2 uv) { w = pow(clamp(w, 0., 1.) * 3.14159 * .5, .9); vec3 c = vec3(sin(w), sin(w * 2.), cos(w)) * 1.1; return mix(texture(iChannel0, uv).rgb, c, w); } void mainImage(out vec4 fragColor, in vec2 fragCoord) { vec2 uv = (2. * fragCoord - iResolution.xy) / min(iResolution.x, iResolution.y); float d = 0.; for (int i = 0; i < PointCount; i++) { vec3 v = Points[i]; float intensity = v.z / HEAT_MAX; float pd = (1. - length(uv - v.xy) / PointRadius) * intensity; d += pow(max(0., pd), 2.); } fragColor = vec4(gradient(d, uv), 1.); } 将上述代码转为unity可读的shader
可以将上述代码转为以下的 Unity Shader:
```
Shader "Custom/HeatMap" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_HeatMax ("Heat Max", Range(0, 100)) = 10
_PointRadius ("Point Radius", Range(0, 1)) = 0.42
_PointCount ("Point Count", Range(0, 50)) = 18
_Points ("Points", Vector) = (
(0, 0, 10),
(0.2, 0.6, 5),
(0.25, 0.7, 8),
(0.33, 0.9, 5),
(0.35, 0.8, 6),
(0.1, 0.1, 6),
(-0.45, 0.8, 4),
(-0.2, -0.6, 5),
(-0.25, -0.7, 8),
(-0.33, -0.9, 8),
(0.35, -0.45, 10),
(-0.1, -0.8, 10),
(0.33, -0.3, 5),
(-0.35, 0.75, 6),
(0.6, 0.4, 10),
(-0.4, -0.8, 4),
(0.7, -0.3, 6),
(0.3, -0.8, 8)
)
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float _HeatMax;
uniform float _PointRadius;
uniform int _PointCount;
uniform float3 _Points[_PointCount];
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
vec3 gradient(float w, vec2 uv) {
w = pow(clamp(w, 0., 1.) * 3.14159 * .5, .9);
vec3 c = vec3(sin(w), sin(w * 2.), cos(w)) * 1.1;
return mix(tex2D(_MainTex, uv).rgb, c, w);
}
void frag (v2f i, out float4 o : SV_Target) {
vec2 uv = (2. * i.uv - _ScreenParams.xy) / min(_ScreenParams.x, _ScreenParams.y);
float d = 0.;
for (int i = 0; i < _PointCount; i++) {
float3 v = _Points[i];
float intensity = v.z / _HeatMax;
float pd = (1. - length(uv - v.xy) / _PointRadius) * intensity;
d += pow(max(0., pd), 2.);
}
o = float4(gradient(d, uv), 1.);
}
ENDCG
}
}
FallBack "Diffuse"
}
```
在这个 Unity Shader 中,我们使用了 _MainTex 来加载纹理,_HeatMax、_PointRadius、_PointCount 和 _Points 分别表示热力图的最大值、点半径、点数量和点的位置。然后在 Pass 中进行了渲染,使用了 vertex 和 uv 的数据类型,同时还使用了 UnityCG.cginc 中的函数。最后,使用了 gradient 函数和 frag 函数来实现对应的渲染效果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)