void main ( ) { float x = 2.0, y; if (x < 0.0) y = 0.0; else if (x < 10.0) y = 1.0 / x; else y = 1.0; printf ("%f\n", y); }
时间: 2023-11-29 15:07:05 浏览: 96
这是一个简单的 C 语言程序,它的作用是计算输入的 x 值对应的 y 值。程序首先定义了一个浮点数变量 x,并将其赋值为 2.0。然后程序使用 if else 语句对 x 进行判断,如果 x 小于 0.0,则 y 被赋值为 0.0;如果 x 大于等于 0.0 且小于 10.0,则 y 被赋值为 1.0/x;如果 x 大于等于 10.0,则 y 被赋值为 1.0。最后程序使用 printf 函数输出变量 y 的值。在这个例子中,由于 x 的值为 2.0,所以 y 的值为 0.5,程序输出的结果为 0.500000。
相关问题
可以提供一个C语言OpenGL编写的卫星发射动画演示代码吗,要非常高级的那种
当然可以,以下是一个基于C语言和OpenGL实现的卫星发射动画演示代码,希望能够满足您的需求:
```c
#include <GL/glut.h>
#include <math.h>
#define PI 3.14159265358979323846
float rocketAngle = 0.0f;
float rocketX = 0.0f;
float rocketY = 0.0f;
float rocketZ = 0.0f;
float rocketHeight = 3.0f;
float rocketWidth = 1.0f;
float rocketDepth = 1.0f;
float rocketFireHeight = 2.0f;
float rocketFireWidth = 0.5f;
float satelliteAngle = 0.0f;
float satelliteX = 0.0f;
float satelliteY = 0.0f;
float satelliteZ = 0.0f;
float satelliteRadius = 3.0f;
float satelliteOrbitRadius = 10.0f;
float satelliteOrbitAngle = 0.0f;
void drawRocket() {
// 绘制火箭身体
glColor3f(0.8f, 0.8f, 0.8f);
glPushMatrix();
glTranslatef(rocketX, rocketY, rocketZ);
glRotatef(rocketAngle, 0.0f, 0.0f, 1.0f);
glScalef(rocketWidth, rocketHeight, rocketDepth);
glutSolidCube(1.0f);
glPopMatrix();
// 绘制火箭尾部火焰
glColor3f(1.0f, 0.5f, 0.0f);
glPushMatrix();
glTranslatef(rocketX, rocketY - rocketHeight / 2, rocketZ);
glRotatef(rocketAngle, 0.0f, 0.0f, 1.0f);
glScalef(rocketFireWidth, rocketFireHeight, rocketFireWidth);
glutSolidCone(1.0f, 1.0f, 16, 16);
glPopMatrix();
}
void drawSatellite() {
// 绘制卫星
glColor3f(0.0f, 0.8f, 0.0f);
glPushMatrix();
glTranslatef(satelliteX, satelliteY, satelliteZ);
glRotatef(satelliteAngle, 0.0f, 1.0f, 0.0f);
glutSolidSphere(1.0f, 16, 16);
glPopMatrix();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0f, 20.0f, 20.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
drawRocket();
drawSatellite();
glutSwapBuffers();
}
void reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float)width / height, 0.1f, 100.0f);
}
void timer(int value) {
// 火箭升空
if (rocketY < 10.0f) {
rocketY += 0.1f;
rocketAngle += 1.0f;
}
// 卫星发射
else if (satelliteOrbitAngle < 2 * PI) {
rocketY = 10.0f;
satelliteOrbitAngle += 0.05f;
satelliteX = satelliteOrbitRadius * cos(satelliteOrbitAngle);
satelliteZ = satelliteOrbitRadius * sin(satelliteOrbitAngle);
satelliteAngle += 2.0f;
}
glutPostRedisplay();
glutTimerFunc(10, timer, 0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Satellite Launch Animation");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(10, timer, 0);
glutMainLoop();
return 0;
}
```
这是一个相对比较高级的卫星发射动画演示,包括火箭升空和卫星发射两个部分。可以通过调整不同的参数来实现更高级的效果。希望这段代码对您有所帮助!
根据基本RRT算法伪代码设计一个应用于stm32单片机的RRT算法程序
以下是一个基本的RRT算法的C语言伪代码,可以用于stm32单片机的开发:
```
// 定义节点结构体
struct Node {
float x;
float y;
int parent;
};
// 定义全局变量
const int MAX_NODES = 1000; // 最大节点数
Node nodes[MAX_NODES]; // 节点数组
int num_nodes = 0; // 当前节点数
float goal_x = 10.0; // 目标点x坐标
float goal_y = 10.0; // 目标点y坐标
float step_size = 0.5; // 步长
float min_dist = 0.1; // 最小距离
float rand_area = 50.0; // 随机点生成区域
int max_iter = 1000; // 最大迭代次数
// 定义函数:计算两点之间的距离
float distance(float x1, float y1, float x2, float y2) {
float dx = x1 - x2;
float dy = y1 - y2;
return sqrt(dx * dx + dy * dy);
}
// 定义函数:生成随机点
int get_random_node() {
float x = rand() / (float)RAND_MAX * rand_area;
float y = rand() / (float)RAND_MAX * rand_area;
Node rnd = {x, y, 0};
for (int i = 0; i < num_nodes; i++) {
if (distance(nodes[i].x, nodes[i].y, x, y) < min_dist) {
return -1;
}
}
nodes[num_nodes] = rnd;
num_nodes++;
return num_nodes - 1;
}
// 定义函数:查找最近节点
int get_nearest_node(float x, float y) {
int nearest = 0;
float min_dist = distance(x, y, nodes[0].x, nodes[0].y);
for (int i = 1; i < num_nodes; i++) {
float dist = distance(x, y, nodes[i].x, nodes[i].y);
if (dist < min_dist) {
nearest = i;
min_dist = dist;
}
}
return nearest;
}
// 定义函数:检查是否到达目标点
bool is_goal_reached(float x, float y) {
return distance(x, y, goal_x, goal_y) <= min_dist;
}
// 定义函数:添加节点
void add_node(int parent, float x, float y) {
Node n = {x, y, parent};
nodes[num_nodes] = n;
num_nodes++;
}
// 定义函数:查找可行节点
bool find_feasible_point(int from_node, float to_x, float to_y, float& new_x, float& new_y) {
float d = distance(nodes[from_node].x, nodes[from_node].y, to_x, to_y);
if (d <= step_size) {
new_x = to_x;
new_y = to_y;
return true;
} else {
float ratio = step_size / d;
new_x = (1 - ratio) * nodes[from_node].x + ratio * to_x;
new_y = (1 - ratio) * nodes[from_node].y + ratio * to_y;
return false;
}
}
// 定义函数:RRT算法主函数
void rrt() {
nodes[0] = {0.0, 0.0, -1};
num_nodes = 1;
for (int i = 0; i < max_iter; i++) {
int rnd_node = get_random_node();
if (rnd_node == -1) {
continue;
}
int nearest_node = get_nearest_node(nodes[rnd_node].x, nodes[rnd_node].y);
float new_x, new_y;
if (find_feasible_point(nearest_node, nodes[rnd_node].x, nodes[rnd_node].y, new_x, new_y)) {
if (is_goal_reached(new_x, new_y)) {
add_node(nearest_node, goal_x, goal_y);
return;
}
int new_node = num_nodes;
add_node(nearest_node, new_x, new_y);
}
}
}
// 主函数
int main() {
rrt();
return 0;
}
```
需要注意的是,上面的代码是伪代码,需要根据实际情况进行修改和调试。同时,由于stm32单片机的硬件资源较为有限,需要特别注意代码的效率和内存占用情况。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)