gpt4 book ai didi

c++ - 更改已创建的 vector

转载 作者:行者123 更新时间:2023-11-28 03:14:54 27 4
gpt4 key购买 nike

我正在做一个绘制太阳系的项目。我只在我的主要功能中创建了一次行星。

int main(int argc,char* argv[])
{

SolarSystem app(argc, argv);// Create 1 Solar System
app.createPlanets();// Create the planets in the Solar System
app.run();

return 0;
}

这是我的 createPlanets()vector<Planet>planets; 设置值的函数 vector 。

void SolarSystem::createPlanets() const
{
//File for texture / Radius / / Orbit / /Tilt angle
planets.push_back(Planet("images/Sun.jpg", 696, 696, 2500.0, 0.0, 0.0 ));//0
planets.push_back(Planet("images/mercury.jpg", 2.44, 2.44, 57910.0, 45740.0, 0.0 ));//1
planets.push_back(Planet("images/venus.jpg", 6.05, 6.05, 108200.0, 107464.0, 177.3));//2
planets.push_back(Planet("images/earth.jpg", 6.37, 6.34, 149600.0, 147102.0, 23.5 ));//3
planets.push_back(Planet("images/moon.jpg", 1.74, 1.73, 384.0, 383.0, 5.145));//4
planets.push_back(Planet("images/mars.jpg", 3.39, 3.37, 227940.0, 207425.0, 25.2 ));//5
planets.push_back(Planet("images/Jupiter.jpg", 69.90, 65.24, 778330.0, 740734.0, 3.1 ));//6
planets.push_back(Planet("images/neptune.jpg", 24.63, 24.08, 4504300.0, 4460608.0, 29.6 ));//7
planets.push_back(Planet("images/pluto.jpg", 1.15, 1.15, 5913520.0, 4475140.0, 29.6 ));//8
}

我正在创建一个菜单,用户可以在其中更改行星的比例,以便可以轻松发现它们(真实的比例使得行星很难被发现)。为了更改大小,我必须进入 vector 并更改其中每个行星对象的半径。

有人知道我该怎么做吗?下面的显示函数每帧绘制行星。

void SolarSystem::display(GLContextData& contextData) const
{


if(showOrbitPath)
displayOrbitPath();


//These variables set each planet's orbit and rotation speed
//Sun Mer Ven Ear Moon Mar Jup Nep Plu
double orbitS[] = {0.0, 4.15, 1.600, 1.00, 13.0, 0.40, 0.08, 0.006, 0.004};
double rotatS[] = {1.0, 0.50, 0.125, 30.0, 1.00, 30.0, 75.0, 40.00, 5.000};

//index counter for variables above
int i = 0;

//Vector iteration
for(std::vector<Planet>::iterator it = planets.begin(); it != planets.end(); ++it)
{
//If the planet being displayed is the sun
if (i == 0)
//Turn off shading
glDisable(GL_LIGHTING);
else //Turn the spot where the Sun is into a light Source
{
GLfloat light_diffuse[] = { 1.5, 1.5, 1.5, 1.5 };
GLfloat pos[] = { 2500.0, 0.0, 0.0, 1.0};
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, pos);

}
if (i == 4)//moon
{
it->displayPlanet(orbitCounter*orbitS[i], orbitCounter*rotatS[i],
planets[3].getMajorAxis(),planets[3].getMinorAxis());
}
else //Planets
it->displayPlanet(orbitCounter*orbitS[i]*0, orbitCounter*rotatS[i], 0.0,0.0);

i++;
}
//base counter used to calculate orbit and rotation speed
orbitCounter+=0.05;
//89750.0 is what's necessary for the Pluto to orbit around the Sun once.
if (orbitCounter > 89750.0)
orbitCounter = 0.0;
}

我想在我可以说的地方添加一个 if 语句

int scale = 1;
if(changeScale)
scale == 100;

然后我会用比例乘以半径。如果changeScale未设置,行星应保持相同大小。如果changeScale被选中会使行星的大小增加 100。有没有一种方法可以做到这一点而不必每一帧都画出行星?如果那不可能,我该如何在每一帧重新绘制行星?我只是不确定如何修改 vector 中的对象。

谢谢!

最佳答案

您可以修改vector 中的Planet。它看起来像这样:

planets[index].member = new_val;

或者,如果变量是 private 并且您有访问器方法,

planets[index].setMember(new_val);

因此,如果您想更改半径,它看起来类似于以下两个语句之一:

if (changeScale) {
for (int i = 0; i < planets.size(); ++i) {
planets[i].radius *= 100; // this
planets[i].setRadius( planets[i].getRadius() * 100 ); // or this
}
}

或者,您可以使用迭代器:

if (changeScale) {
for (std::vector<Planet>::iterator it = planets.begin(); it != planets.end(); ++it) {
(*it).radius *= 100; // this
(*it).setRadius( (*it).getRadius() * 100 ); // or this
}
}

关于c++ - 更改已创建的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17220363/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com