int dy = p.y() - _gravity.y(); int dx = p.x() - _gravity.x(); qint64 key = (dist << 32) + (dy << 16) + dx;
时间: 2024-05-17 22:14:29 浏览: 11
这几行代码是计算每个扩展(_expns)的排序关键字,以便对它们进行排序。其中,dy 和 dx分别表示扩展相对重心的 y 坐标和 x 坐标的偏移量,dist 是扩展和重心之间的曼哈顿距离(ManhattanLength),计算曼哈顿距离的函数在这段代码中没有给出。最后,将这三个值组合成一个qint64类型的排序关键字key,其中dist占据高32位,dy占据中间16位,dx占据低16位。
相关问题
编译器:C++ (g++) Particle类型用于表示处于三维空间中的质点,其包括如下成员:① 三个浮点数成员x、y和z表示质点在三维空间中的坐标;② 浮点数成员mass表示质点的质量;③ 接受空间坐标和质量参数的构造函数;④ 自定义operator-()操作符函数用于计算两个质点间的万有引力,即表达式p1 – p2调用执行p1的operator-()操作符函数,计算质点p1和p2间的万有引力。 万有引用公式F = GMm/r²中,万有引力常数G = 6.67×10-11N·m²/kg²。 请依据上述描述,设计Particle类,使得下述代码能正常执行。 注意:天文数字都很大,建议浮点数全部采用long double类型。 裁判测试程序样例: #include <iostream> #include <cmath> using namespace std; //在此处定义Particle类及其重载operator+()操作符函数 int main() { long double x, y, z, mass; cin >> x >> y >> z >> mass; Particle p1(x,y,z,mass); cin >> x >> y >> z >> mass; const Particle p2(x,y,z,mass); long double force = p1 - p2; printf("Gravity between two objects = %.1Lf N",force); return 0; } 输入样例: 0 0 0 5.965e15 0 0 38400000 7.342e14 输出样例: Gravity between two objects = 198101.7 N 说明:输入输出中的所有数据均为标准单位,坐标为米,质量为kg。 请注意:函数题只需要提交相关代码片段,不要提交完整程序。
代码如下:
```c++
#include <iostream>
#include <cmath>
using namespace std;
class Particle{
private:
long double x, y, z, mass;
public:
Particle(long double x=0, long double y=0, long double z=0, long double mass=0){
this->x = x;
this->y = y;
this->z = z;
this->mass = mass;
}
long double operator-(const Particle& p2){
const long double G = 6.67e-11;
long double dx = x - p2.x;
long double dy = y - p2.y;
long double dz = z - p2.z;
long double r = sqrt(dx*dx + dy*dy + dz*dz);
return G * mass * p2.mass / (r * r);
}
};
int main(){
long double x, y, z, mass;
cin >> x >> y >> z >> mass;
Particle p1(x,y,z,mass);
cin >> x >> y >> z >> mass;
const Particle p2(x,y,z,mass);
long double force = p1 - p2;
printf("Gravity between two objects = %.1Lf N",force);
return 0;
}
```
python 下面八个块显示鼠标追踪位置turtle.canvas 重力四子棋 turtle 代码
1. 显示鼠标追踪位置:
```python
import turtle
def track_mouse(x, y):
turtle.goto(x, y)
turtle.onscreenclick(track_mouse)
turtle.mainloop()
```
2. turtle.canvas:
```python
import turtle
turtle.getcanvas().postscript(file="canvas.eps")
turtle.mainloop()
```
3. 重力:
```python
import turtle
GRAVITY = 0.1
class Ball(turtle.Turtle):
def __init__(self, x, y, dx, dy, radius, color):
super().__init__(shape="circle")
self.penup()
self.setposition(x, y)
self.dx = dx
self.dy = dy
self.radius = radius
self.color(color)
def move(self):
self.dy -= GRAVITY
self.sety(self.ycor() + self.dy)
self.setx(self.xcor() + self.dx)
class Game:
def __init__(self):
self.window = turtle.Screen()
self.window.setup(800, 600)
self.window.bgcolor("white")
self.balls = []
def add_ball(self, x, y, dx, dy, radius, color):
ball = Ball(x, y, dx, dy, radius, color)
self.balls.append(ball)
def start(self):
while True:
for ball in self.balls:
ball.move()
if ball.ycor() - ball.radius < -300:
ball.dy = -ball.dy
if ball.xcor() + ball.radius > 400 or ball.xcor() - ball.radius < -400:
ball.dx = -ball.dx
self.window.update()
game = Game()
game.add_ball(0, 200, 0, 0, 30, "red")
game.add_ball(0, 0, 5, 0, 20, "blue")
game.add_ball(0, -200, -5, 0, 10, "green")
game.start()
```
4. 四子棋:
```python
import turtle
class Board:
def __init__(self):
self.board = [[None for _ in range(7)] for _ in range(6)]
def is_full(self):
for row in self.board:
for cell in row:
if cell is None:
return False
return True
def is_winner(self, player):
# Check rows
for row in self.board:
count = 0
for cell in row:
if cell == player:
count += 1
if count == 4:
return True
else:
count = 0
# Check columns
for j in range(7):
count = 0
for i in range(6):
if self.board[i][j] == player:
count += 1
if count == 4:
return True
else:
count = 0
# Check diagonals
for i in range(3):
for j in range(4):
if (self.board[i][j] == player and self.board[i+1][j+1] == player and
self.board[i+2][j+2] == player and self.board[i+3][j+3] == player):
return True
for i in range(3):
for j in range(3, 7):
if (self.board[i][j] == player and self.board[i+1][j-1] == player and
self.board[i+2][j-2] == player and self.board[i+3][j-3] == player):
return True
return False
def play(self, player, column):
for i in range(5, -1, -1):
if self.board[i][column] is None:
self.board[i][column] = player
return True
return False
class Game:
def __init__(self):
self.window = turtle.Screen()
self.window.setup(700, 600)
self.window.bgcolor("white")
self.board = Board()
self.turn = "red"
self.draw_board()
self.window.onclick(self.play)
def draw_board(self):
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
pen.penup()
pen.setposition(-300, -250)
pen.pendown()
pen.pensize(3)
for _ in range(2):
pen.forward(600)
pen.left(90)
pen.forward(500)
pen.left(90)
pen.penup()
pen.setposition(-280, -225)
for column in range(7):
pen.write(column, align="center", font=("Arial", 16, "normal"))
pen.forward(80)
pen.hideturtle()
def play(self, x, y):
column = int((x + 300) // 80)
if self.board.play(self.turn, column):
self.draw_piece(column)
if self.board.is_winner(self.turn):
self.window.onclick(None)
turtle.color(self.turn)
turtle.write(f"{self.turn.upper()} wins!", align="center", font=("Arial", 32, "bold"))
elif self.board.is_full():
self.window.onclick(None)
turtle.write("Draw!", align="center", font=("Arial", 32, "bold"))
else:
self.turn = "yellow" if self.turn == "red" else "red"
def draw_piece(self, column):
pen = turtle.Turtle()
pen.hideturtle()
pen.penup()
pen.color(self.turn)
row = max([i for i, cell in enumerate(self.board.board) if cell[column] is None])
pen.setposition(-240 + column * 80, -200 + row * 80)
pen.pendown()
pen.begin_fill()
pen.circle(30)
pen.end_fill()
game = Game()
turtle.mainloop()
```
5. turtle 代码:
```python
import turtle
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()
def draw_triangle(x, y, size, color):
pen.penup()
pen.setposition(x, y)
pen.pendown()
pen.color(color)
pen.begin_fill()
for _ in range(3):
pen.forward(size)
pen.left(120)
pen.end_fill()
draw_triangle(-200, 0, 100, "red")
draw_triangle(0, 0, 100, "green")
draw_triangle(200, 0, 100, "blue")
turtle.mainloop()
```
相关推荐
![](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)