Windows Phone开发(33):路径之其它Geometry

简单几何图形绘制
本文介绍如何使用LineGeometry、RectangleGeometry、EllipseGeometry及GeometryGroup等元素在XAML中绘制基本几何图形,包括线段、矩形、椭圆及其组合。

上一节中,我们把最复杂的PathGeometry给干了,生剩下几个家伙就好办事了。一起来见见他们的真面目吧。

 

一、LineGeometry

 

这个几何图形就很简单了,一条线段,两个点——StartPoint And EndPoint。

一起来看看下面的例子。

        <Path Grid.Column="0" Grid.Row="0">
            <Path.Data>
                <LineGeometry StartPoint="20,5" EndPoint="200,320"/>
            </Path.Data>
        </Path>


 

运行之后你会看到以下情景:

 

 

 

 

二、RectangleGeometry

它呈现一人矩形的几何图形,Rect指示其中矩形的位置大小,在XAML中可以用4个数值表示,即X、Y、Width、Height;别外,RadiusX和RadiusY表示圆角在X轴和Y轴上的半径。看下面的例子。

        <Path Grid.Column="1" Grid.Row="0">
            <Path.Data>
                <RectangleGeometry Rect="12,6,125,90" RadiusX="24" RadiusY="30"/>
            </Path.Data>
        </Path>


运行效果如下图所示。

 

 

 

 

三、EllipseGeometry

 

表示一个椭圆的几何图形,Center属性为椭圆的中心点的坐标,RadiusX和RadiusY分别为X轴方向上和Y轴方向上的半径长度。看例子。

        <Path Grid.Column="0" Grid.Row="1">
            <Path.Data>
                <EllipseGeometry Center="100,180" RadiusX="55" RadiusY="120"/>
            </Path.Data>
        </Path>


 

运行效果如下:

 

 

 

四、GeometryGroup

 

严格上说,它不属性一种几何图形,但它很有用,因为它可以同时包含N个几何图形,如下面例子所示。

        <Path Grid.Column="1" Grid.Row="1">
            <Path.Data>
                <GeometryGroup>
                    <LineGeometry StartPoint="32,185" EndPoint="180,230"/>
                    <RectangleGeometry Rect="35,85,136,96" RadiusX="25" RadiusY="5"/>
                    <EllipseGeometry Center="112,130" RadiusX="45" RadiusY="36"/>
                </GeometryGroup>
            </Path.Data>
        </Path>


运行效是如下所示:

 

 

 

下面是本节示例的完整XAML代码。

<phone:PhoneApplicationPage 
    x:Class="Sample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <Style TargetType="Path">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Stretch"/>
            <Setter Property="Margin" Value="20"/>
            <Setter Property="Stroke" Value="Blue"/>
            <Setter Property="StrokeThickness" Value="8"/>
        </Style>
    </phone:PhoneApplicationPage.Resources>
    
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Path Grid.Column="0" Grid.Row="0">
            <Path.Data>
                <LineGeometry StartPoint="20,5" EndPoint="200,320"/>
            </Path.Data>
        </Path>
        <Path Grid.Column="1" Grid.Row="0">
            <Path.Data>
                <RectangleGeometry Rect="12,6,125,90" RadiusX="24" RadiusY="30"/>
            </Path.Data>
        </Path>
        <Path Grid.Column="0" Grid.Row="1">
            <Path.Data>
                <EllipseGeometry Center="100,180" RadiusX="55" RadiusY="120"/>
            </Path.Data>
        </Path>
        <Path Grid.Column="1" Grid.Row="1">
            <Path.Data>
                <GeometryGroup>
                    <LineGeometry StartPoint="32,185" EndPoint="180,230"/>
                    <RectangleGeometry Rect="35,85,136,96" RadiusX="25" RadiusY="5"/>
                    <EllipseGeometry Center="112,130" RadiusX="45" RadiusY="36"/>
                </GeometryGroup>
            </Path.Data>
        </Path>
    </Grid>
</phone:PhoneApplicationPage>


 

### 使用方法 #### 1. 包含必要的头文件 要使用 `std::vector<geometry_msgs::msg::Pose>`,需要包含 `<vector>` 和 `geometry_msgs/msg/pose.hpp` 头文件。 ```cpp #include <vector> #include <geometry_msgs/msg/pose.hpp> ``` #### 2. 创建 `std::vector<geometry_msgs::msg::Pose>` 对象 ```cpp std::vector<geometry_msgs::msg::Pose> poseVector; ``` #### 3. 向向量中添加元素 可以使用 `push_back` 方法向向量中添加 `geometry_msgs::msg::Pose` 元素。 ```cpp geometry_msgs::msg::Pose pose; pose.position.x = 1.0; pose.position.y = 2.0; pose.position.z = 3.0; pose.orientation.x = 0.0; pose.orientation.y = 0.0; pose.orientation.z = 0.0; pose.orientation.w = 1.0; poseVector.push_back(pose); ``` #### 4. 访问向量中的元素 可以使用下标运算符 `[]` 或迭代器来访问向量中的元素。 ```cpp // 使用下标运算符 geometry_msgs::msg::Pose firstPose = poseVector[0]; // 使用迭代器 for (auto it = poseVector.begin(); it != poseVector.end(); ++it) { geometry_msgs::msg::Pose currentPose = *it; // 处理当前位姿 } ``` #### 5. 遍历向量 除了使用迭代器,还可以使用范围 for 循环遍历向量。 ```cpp for (const auto& pose : poseVector) { // 处理当前位姿 } ``` ### 应用场景 #### 1. 路径规划 在机器人路径规划中,`std::vector<geometry_msgs::msg::Pose>` 可以用来存储机器人从起始点到目标点的一系列中间位姿,这些位姿构成了机器人的运动路径。例如,在全局路径规划算法(如 A* 算法)中,算法会计算出一系列的位姿,将这些位姿存储在 `std::vector<geometry_msgs::msg::Pose>` 中,然后机器人可以按照这个路径依次移动到每个位姿点。 #### 2. 位姿采样 在机器人运动规划和控制中,有时需要对机器人的位姿进行采样。可以生成一系列不同的位姿,并将它们存储在 `std::vector<geometry_msgs::msg::Pose>` 中,然后对这些采样位姿进行评估,选择最优的位姿作为机器人的下一步运动目标。 #### 3. 数据记录和回放 在机器人实验和调试过程中,可能需要记录机器人在一段时间内的位姿信息。可以将机器人的位姿信息依次存储在 `std::vector<geometry_msgs::msg::Pose>` 中,然后将这些数据保存到文件中。在需要回放实验数据时,可以从文件中读取这些位姿信息,重新加载到 `std::vector<geometry_msgs::msg::Pose>` 中,让机器人按照记录的位姿进行运动。 ### 代码示例 ```cpp #include <iostream> #include <vector> #include <geometry_msgs/msg/pose.hpp> int main() { std::vector<geometry_msgs::msg::Pose> poseVector; geometry_msgs::msg::Pose pose; pose.position.x = 1.0; pose.position.y = 2.0; pose.position.z = 3.0; pose.orientation.x = 0.0; pose.orientation.y = 0.0; pose.orientation.z = 0.0; pose.orientation.w = 1.0; poseVector.push_back(pose); for (const auto& pose : poseVector) { std::cout << "Position: (" << pose.position.x << ", " << pose.position.y << ", " << pose.position.z << ")" << std::endl; std::cout << "Orientation: (" << pose.orientation.x << ", " << pose.orientation.y << ", " << pose.orientation.z << ", " << pose.orientation.w << ")" << std::endl; } return 0; } ```
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值