omnet++ 之canvas示例 在NED 文件中演示 @figure 用法

2023-10-10 15:08

本文主要是介绍omnet++ 之canvas示例 在NED 文件中演示 @figure 用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在NED文件中如果涉及到图的定义,在canvas示例中的代码如下:

network CarDemo
{parameters:@display("bgb=800,500");@figure[road] (type=polygon; points=100,200, 200,100, 300,50, 500,50, 650,100, 700,200, 600,300, 500,350, 400,350, 200,350, 100,300; smooth=true; lineWidth=60; zoomLineWidth=true; lineColor=grey);@figure[paint](type=polygon; points=100,200, 200,100, 300,50, 500,50, 650,100, 700,200, 600,300, 500,350, 400,350, 200,350, 100,300; smooth=true; lineWidth=3; zoomLineWidth=true; lineColor=white; lineStyle=dashed);@figure[trail](type=polyline; lineWidth=5; lineOpacity=0.6; lineColor=orange);@figure[car](type=image; image="car-top-view"; pos=0,0; anchor=c);@figure[car.antenna](type=image; image="antenna"; pos=-10,-40; anchor=nw);@figure[car.antenna.beam](type=path; path="M 10 0 L 10 -20 L 1200 -100 L 1200 100 L 10 20 Z"; fillColor=blue; fillOpacity=0.1; lineColor=blue; lineOpacity=0.1);@figure[status](type=group; transform=translate(700,20));// or, for non-zooming status area: @figure[status](type=panel; pos=700,20);@figure[status.bg](type=rectangle; pos=0,0; size=150,100; anchor=n; cornerRadius=10; fillColor=#fafdb9; fillOpacity=0.2; lineColor=#fafdb9; lineOpacity=0.4);@figure[status.distanceTitle](type=text; pos=0,20; text="Distance Travelled"; anchor=center);@figure[status.distance](type=text; pos=0,40; text="0m"; anchor=center; font=Arial,22);@figure[status.headingTitle](type=text; pos=0,70; text="Heading"; anchor=center);@figure[status.heading](type=text; pos=0,90; text="0"; anchor=center; font=Arial,22);submodules:animator: CarAnimator;
}

其中@figure的定义表示一个图,

在c++中可以引用:


void CarAnimator::initialize()
{timeStep = 1;           // 步长,步长 * 车速 = 一次移动距离speed = 2;              // 车速heading = 0;            // 车头角度angularSpeed = 0;       // 转向速度targetPointIndex = 0;   // 记录公路数据的索引distanceTravelled = 0;  // 总里程cCanvas *canvas = getParentModule()->getCanvas();canvas->setAnimationSpeed(50.0, this);// 取得公路的参数,road = check_and_cast<cPolygonFigure *>(canvas->getFigure("road"));// 拖尾数据,缺少点,需要在运动时候计算trail = check_and_cast<cPolylineFigure *>(canvas->getFigure("trail"));// 图形,@figure[car](type=image; image="car-top-view"; pos=0,0; anchor=c);car = check_and_cast<cImageFigure *>(canvas->getFigure("car"));// 天线的图形 @figure[car.antenna](type=image; image="antenna"; pos=-10,-40; anchor=nw);antenna = check_and_cast<cFigure *>(canvas->getFigureByPath("car.antenna"));// 右侧显示行驶距离文本distanceDisplay = check_and_cast<cTextFigure *>(canvas->getFigureByPath("status.distance"));// 右侧显示行驶的车头角度headingDisplay = check_and_cast<cTextFigure *>(canvas->getFigureByPath("status.heading"));// 获取下一点的数据loc = road->getPoint(targetPointIndex);// 引擎内部监控各个变量WATCH(timeStep);WATCH(loc.x);WATCH(loc.y);WATCH(speed);WATCH(heading);WATCH(angularSpeed);WATCH(targetPointIndex);WATCH(distanceTravelled);refresh();// 设置下次事件scheduleAt(simTime(), new cMessage());
}void CarAnimator::refresh() const
{double t = (simTime() - lastStep) / timeStep;ASSERT(t >= 0);ASSERT(t <= 1);// 车转向cFigure::Transform carTr;carTr.rotate(heading + angularSpeed * t);// 车移动double distance = speed * t;carTr.translate(loc.x + distance * cos(heading), loc.y + distance * sin(heading));car->setTransform(carTr);// 天线转向cFigure::Transform antTr;antTr.rotate(-2 * simTime().dbl()*M_PI/180);antenna->setTransform(antTr);// 设置状态信息char buf[20];sprintf(buf, "%.0fm", distanceTravelled);distanceDisplay->setText(buf);int degrees = -int(heading*180/M_PI);degrees = degrees - 360 * (int)floor(degrees / 360.0);sprintf(buf, "%d\xC2\xB0", degrees);headingDisplay->setText(buf);
}void CarAnimator::refreshDisplay() const
{// 其实,如果注释了这一句,也能运行,因为每个消息到达时候都进行了一个运动计算。// 但是,注释后,明显会发生顿挫感,主要是因为:?refresh();
}void CarAnimator::handleMessage(cMessage *msg)
{// 一次移动距离 =  步长,步长 * 车速double distance = speed * timeStep.dbl();// 根据角度更新当前位置loc.x += distance * cos(heading);loc.y += distance * sin(heading);// 计算下一个目标的方向Point target = road->getPoint(targetPointIndex);Point vectorToTarget = target - loc;// 距离小于某个数,则认为到了,if (vectorToTarget.getLength() < 50)  // reachedtargetPointIndex = (targetPointIndex+1) % road->getNumPoints();double targetDirection = atan2(vectorToTarget.y, vectorToTarget.x);double diff = targetDirection - heading;while (diff < -M_PI)diff += 2*M_PI;while (diff > M_PI)diff -= 2*M_PI;// ,计算车头角度heading += angularSpeed * timeStep.dbl();// 以1/30的相差角度作为一次转向速度,转向angularSpeed = diff / 30;distanceTravelled += distance;refresh();// tail作为一个队列使用,多了就弹出一个,少了就添加trail->addPoint(loc);if (trail->getNumPoints() > 500)trail->removePoint(0);lastStep = simTime();scheduleAt(simTime() + timeStep, msg);
}

其中,@figure的类型有如下几种:

@fifigure type

C++ class

中文含义

line

cLineFigure

直线

arc

cArcFigure

弧线

polyline

cPolylineFigure

折线

rectangle

cRectangleFigure

矩形

oval

cOvalFigure

椭圆

ring

cRingFigure

圆环

pieslice

cPieSliceFigure

饼图部分

polygon

cPolygonFigure

多边形

path

cPathFigure

路径

text

cTextFigure

路径

label

cLabelFigure

文本标签

image

cImageFigure

图像

icon

cIconFigure

图标

pixmap

cPixmapFigure

位图

group

cGroupFigure

 

每个图由一组属性参数来定义,属性的类型如下表:

类型

值定义格式

备注

bool

true 或者false.

 

int

整数

 

double

实数

 

double01

[0,1]之间的实数

比如透明度,不透明度

degrees

表示温度的实数

 

string

字符串

如果它包含逗号、分号、右括号或其他影响解析的字符,需要使用引号括起来

Anchor

c, center, n, e, s, w, nw, ne, se, sw, start, middle, or end.

text fifigures只能用后三个

Arrowhead :

CapStyle

none, simple, triangle, or barbed.

butt, square, or round.

箭头

Color :

 

A color in HTML format (#rrggbb), a color in HSB format (@hhssbb), or a valid SVG color name.

 

 

Dimensions :

width, height

Size given as width and height.

FigureType :

各种内置图类型

Register_Figure()

FillRule

evenodd or nonzero.

 

 

Font

typeface, size, style

 

All three items are optional. size is the font size in points. style is space-sparated list of

zero or more of the following words: normal, bold, italic, underline.

 

ImageName :

文件名

The name of an image.

Interpolation

none, fast, or best

 

JoinStyle

bevel, miter, or round

 

LineStyle

solid, dotted, or dashed

 

Point

x, y

一个点

Point2

x1, y1, x2, y2

2个点

PointList

x1, y1, x2, y2, x3, y3...

点列表

Rectangle

x, y, width, height

矩形,左上角,长宽

TagList

tag1, tag2, tag3...

 

Tint

Color, double01

Specififies tint color and the amount of tinting for images.

Transform

translate(x, y),

rotate(deg),

rotate(deg, centerx, centery),

scale(s), scale(sx, sy),

scale(s, centerx, centery),

scale(sx, sy, centerx, centery),

skewx(coeff),

skewx(coeff, centery),

skewy(coeff),

skewy(coeff, centerx),

matrix(a, b, c, d, t1, t2)

 

一个或者多个转换组成的步骤列表

由上面表格的各种类型的属性一起可以定义某个图。不同的图包含的属性不同:

类型:父类

值格式定义

备注

(fifigure) :

 

type=<FigureType>; visible=<bool>; tags=<TagList>; childZ=<int>;transform=<Transform>;

基类

(abstractLine) : fifigure

 

lineColor=<Color>; lineStyle=<LineStyle>; lineWidth=<double>;

lineOpacity=<double>; capStyle=<CapStyle>; startArrowhead=<Arrowhead>;

endArrowhead=<Arrowhead>; zoomLineWidth=<bool>;

抽象类

line : abstractLine

points=<Point2>

直线

arc : abstractLine

bounds=<Rectangle> pos=<Point>; size=<Dimensions>; anchor=<Anchor>;

startAngle=<degrees>; endAngle=<degrees>

弧线

polyline : abstractLine

points=<PointList>; smooth=<bool>; joinstyle=<JoinStyle>

折线

(abstractShape) : fifigure

 

lineColor=<Color>; fillColor=<Color>; lineStyle=<LineStyle>;

lineWidth=<double>; lineOpacity=<double01>; fillOpacity=<double01>;

zoomLineWidth=<bool>

图形类

rectangle : abstractShape

 

bounds=<Rectangle> pos=<Point>; size=<Dimensions>; anchor=<Anchor>;

cornerRadius=<double>|<Dimensions>

矩形

oval : abstractShape

 

bounds=<Rectangle> pos=<Point>; size=<Dimensions>; anchor=<Anchor>

 

椭圆

ring : abstractShape

 

bounds=<Rectangle> pos=<Point>; size=<Dimensions>; anchor=<Anchor>;

innerSize=<Dimensions>

 

圆环

pieslice : abstractShape

 

bounds=<Rectangle> pos=<Point>; size=<Dimensions>; anchor=<Anchor>;

startAngle=<degrees>; endAngle=<degrees>

 

饼图部分

polygon : abstractShape

 

points=<PointList>; smooth=<bool>; joinStyle=<JoinStyle>; fillRule=<FillRule>

 

多边形

path : abstractShape

 

path=<string>; offset=<Point>; joinStyle=<JoinStyle>; capStyle=<CapStyle>;

fillRule=<FillRule>

路径

(abstractText) : figure

 

pos=<Point>; anchor=<Anchor> text=<string>; font=<Font>; opacity=<double01>;

color=<Color>;

 

抽象文字

label : abstractText

 

angle=<degrees>;

 

标签

text : abstractText

 

 

文本框

(abstractImage) : figure

 

bounds=<Rectangle> pos=<Point>; size=<Dimensions>; anchor=<Anchor>;

interpolation=<Interpolation>; opacity=<double01>; tint=<Tint>

 

抽象图片

image : abstractImage

image=<ImageName>

 

图像

icon : abstractImage

image=<ImageName>

图标

pixmap : abstractImage

resolution=<Dimensions>

位图

 

 

这篇关于omnet++ 之canvas示例 在NED 文件中演示 @figure 用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/181235

相关文章

MySQL 主从复制部署及验证(示例详解)

《MySQL主从复制部署及验证(示例详解)》本文介绍MySQL主从复制部署步骤及学校管理数据库创建脚本,包含表结构设计、示例数据插入和查询语句,用于验证主从同步功能,感兴趣的朋友一起看看吧... 目录mysql 主从复制部署指南部署步骤1.环境准备2. 主服务器配置3. 创建复制用户4. 获取主服务器状态5

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I