【id:34】【20分】D. Point_Array(类+构造+对象数组)

题目描述

上面是我们曾经练习过的一个习题,请在原来代码的基础上作以下修改:1、增加自写的析构函数;2、将getDisTo方法的参数修改为getDisTo(const Point &p);3、根据下面输出的内容修改相应的构造函数。

然后在主函数中根据用户输入的数目建立Point数组,求出数组内距离最大的两个点之间的距离值。

输入

测试数据的组数 t

第一组点的个数

第一个点的 x 坐标   y坐标

第二个点的 x坐标  y坐标

............

输出

输出第一组距离最大的两个点以及其距离(存在多个距离都是最大值的情况下,输出下标排序最前的点组合。比如如果p[0]和p[9]、p[4]和p[5]之间的距离都是最大值,那么前一个是答案,因为p[0]排序最前)

...........

在C++中,输出指定精度的参考代码如下:

#include <iostream>

#include <iomanip> //必须包含这个头文件

using namespace std;

void main( )

{ double a =3.141596;

  cout<<fixed<<setprecision(3)<<a<<endl;  //输出小数点后3位


输入样例

2
4
0 0
5 0
5 5
2 10
3
-1 -8
0 9
5 0
 


输出样例

Constructor.
Constructor.
Constructor.
Constructor.
The longeset distance is 10.44,between p[1] and p[3].
Distructor.
Distructor.
Distructor.
Distructor.
Constructor.
Constructor.
Constructor.
The longeset distance is 17.03,between p[0] and p[1].
Distructor.
Distructor.
Distructor.
 


#include <iomanip>
#include <iostream>
#include <math.h>
using namespace std;
class Point
{
private:
    double x, y;
public:
    Point() {}
   
    //将xy存入对象point里面 并输出"Constructor." 
    void setXY(double x1, double y1) { x = x1, y = y1; cout << "Constructor." << endl; }
    
    //求两点距离
    double getDisTo(const Point& p);

    ~Point()
    {
        cout << "Distructor.\n";
    }
};

//求两点距离
double Point::getDisTo(const Point& p)
{
    double d;
    d = sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
    return d;
}
int main()
{
    int t;
    int  n, i = 0; double x, y;
    cin >> t;
    while (t--)
    {
        cin >> n;//一组n个点
        Point* p = new Point[n];//创建一个动态数组(类) 但是是类

        for (int i = 0; i < n; i++)
        {
            cin >> x >> y;
            p[i].setXY(x, y);//给每个类赋值xy
        }
        double max = 0; 
        int maxindex = 0, minindex = 0;
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                if (max < p[i].getDisTo(p[j]))  //求最长距离
                {
                    max = p[i].getDisTo(p[j]);
                    maxindex = i; minindex = j;//获取下标
                }
            }
        } 
        //0-1 0-2 0-3 .....0-(n-1)
        //1-2 1-3 ...1-(n-1)
        //......
        //(n-2)-(n-1)
        
        // fixed << setprecision(2) 保留两位小数
        cout << fixed << setprecision(2) << "The longeset distance is " << max
            << ",between p[" << maxindex << "] and p[" << minindex << "].\n";
        delete[]p;//删除动态数组 
    }
    return 0;
}

<< 根据你的需求,这段代码实现了加载高德地图(AMap)并设置当前地理位置的功能,并且支持监听点击事件、动态更新路径等操。以下是对关键部的详细解释以及如何正确地将用户当前位置显示到地图上的解决方案。 ### 解决方案及改进 1. **获取用户的地理信息** 使用 `AMap.Geolocation` 插件获取用户的实时位置。 2. **标记用户的位置** 在获取到用户坐标后,在地图上使用 `marker` 标记该位置。 3. **完整逻辑实现** - 首先调用 `Geolocation` 获取用户的经纬度; - 将获取的经纬度为中心点设置为地图视图; - 动态创建一个 Marker 并绑定至地图; 下面是完整的 Vue 实现: ```vue <template> <div id="container" style="width: 100%; height: 500px;"></div> </template> <script> export default { data() { return { map: null, current_position: [], markers: [] }; }, async mounted() { try { const AMap = await window.AMapLoader.load({ key: "your_api_key", // 替换为你自己的API Key version: "2.0", }); // 初始化地图实例 this.map = new AMap.Map("container", { viewMode: "3D", zoom: 13, center: [103.808299, 34.791787], // 初始中心点 }); // 加载插件 AMap.plugin( ["AMap.Scale", "AMap.HawkEye", "AMap.Geolocation", "AMap.PlaceSearch"], () => { this.map.addControl(new AMap.Scale()); this.map.addControl(new AMap.HawkEye()); // Geolocation 定位功能 const geolocationCtrl = new AMap.Geolocation({ enableHighAccuracy: true, // 是否启用高精度定位,默认:true timeout: 10000, // 超过10秒后停止定位,默认:无穷大 buttonOffset: new AMap.Pixel(10, 20), // 控制按钮与地图边缘的距离 showMarker: true, // 定位成功后是否在定位到的位置显示标注,默认true }); this.map.addControl(geolocationCtrl); // 开始定位并处理结果 geolocationCtrl.getCurrentPosition((status, result) => { if (status === 'complete') { this.setCurrentLocation(result.position); } else { console.error('Failed to get location:', result); } }); // 添加点击事件 this.map.on("click", (e) => { this.current_position = [e.lnglat.lng, e.lnglat.lat]; this.path.push(this.current_position); this.addMarkers(); }); } ); } catch (error) { console.error("Error loading AMap:", error); } }, methods: { setCurrentLocation(position) { // 更新地图中心点为用户当前位置 this.map.setCenter([position.getLng(), position.getLat()]); // 创建或更新标记 if (!this.userMarker || !this.userMarker.getMap()) { this.userMarker = new AMap.Marker({ icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png", position: [position.getLng(), position.getLat()], title: "Current Position" }); this.userMarker.setMap(this.map); } else { this.userMarker.setPosition([position.getLng(), position.getLat()]); } }, addMarkers() { // 如果已经有markers,则清除它们 if (Array.isArray(this.markers)) { this.markers.forEach(marker => marker.setMap(null)); } // 新建markers数组并添加到map中 this.markers = []; for (const point of this.path) { const marker = new AMap.Marker({ position: point, title: `${point[0]}, ${point[1]}` }); marker.setMap(this.map); this.markers.push(marker); } } } }; </script> ``` --- ### 给出解释: #### 关键步骤解析: 1. **加载高德地图 API 和初始化地图实例**: ```js const AMap = await window.AMapLoader.load({...}); ``` 这里通过 `window.AMapLoader.load()` 异步加载了高德地图 SDK 的基础模块,并返回了一个 `AMap` 对象供后续使用。 2. **配置 Geolocation 插件进行用户定位:** ```js const geolocationCtrl = new AMap.Geolocation({...}); this.map.addControl(geolocationCtrl); geolocationCtrl.getCurrentPosition(...); ``` 我们首先创建了一个 `Geolocation` 控制器用于求设备的 GPS 或网络 IP 地址,并将其附加到了地图控制面板中。随后我们调用了其异步接口 `getCurrentPosition` 来尝试获取用户的实际位置数据。 3. **根据用户位置刷新地图中心点和 Marker:** 当接收到有效的用户位置时 (`result.position`) ,我们将执行回调函数以完成如下任务: - 把新的坐标设为地图的视觉焦点(`setCenter`); - 构造一个新的图标表示“我的位置”并放置于相应区域; 4. **响应式捕获鼠标交互行为:** 每当发生一次鼠标的左击动时会触发指定 handler 函数记录下最新选定的目标地点同时生成对应的地标符号. --- ### 注意事项: - 确保替换了正确的 `apiKey`; - 测试前检查浏览器是否开启了对 HTTPS 页面的支持及其权限管理策略; ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值