"nodejs ui"; require("rhino").install(); const ui = require("ui"); class MainActivity extends ui.Activity { constructor() { super(); } get layoutXmlFile() { return "layout.xml"; } onContentViewSet() {} } ui.setMainActivity(MainActivity);
class Hexagon {}
至于需要的属性, 有需求的时候再写
<column> <canvas id="canvas" w="*" h="*"> </canvas> </column>
onContentViewSet(view) { const canvasView = view.binding.canvas; canvasView.on("draw", (canvas) => { canvas.drawColor(canvasBgColor); }); }
let canvasWidth = canvasView.getWidth(); let canvasHeight = canvasView.getHeight(); let centerX = canvasWidth / 2; let centerY = canvasHeight / 2; canvasView.on("draw", (canvas) => { canvas.drawColor(canvasBgColor); canvas.drawPoint(centerX, centerY, paint); });
这个方法应该属于六边形这个类
getSixPoints() { let points = []; let angle = 0; for (let i = 0; i < 6; i++) { let x = this.centerX + this.sideLength * Math.cos(angle); let y = this.centerY - this.sideLength * Math.sin(angle); points.push({ x, y }); angle += Math.PI / 3; } return points; }
这个也属于六边形的方法
getPath() { const path = new Path(); let points = this.getSixPoints(); path.moveTo(points[0].x, points[0].y); for (let i = 1; i < points.length; i++) { path.lineTo(points[i].x, points[i].y); } path.close(); return path; }
let hexagon = new Hexagon(centerX, centerY, config.sideLength); let hexagonPath = hexagon.getPath(); canvasView.on("draw", (canvas) => { canvas.drawColor(canvasBgColor); canvas.drawPoint(centerX, centerY, paint); canvas.drawPath(hexagonPath, paint); });
要绘制一排, 那么六边形左右两边需要是数直的, 把 getSixPoints 方法里的 sin 和 cos 对调一下即可
let x = this.centerX + this.sideLength * Math.sin(angle); let y = this.centerY - this.sideLength * Math.cos(angle);
所有六边形都要在画板内, 我们画左上角第一个六边形;
计算中心点要考虑的条件
let angle = Math.PI / 3; let firstHexagonCenterX = config.sideLength * Math.sin(angle) + config.paintConfig.width; let firstHexagonCenterY = config.sideLength + config.paintConfig.width;
两个挨着的竖边, 应该只需要画一条, 那么来计算第二个六边形的中心位置, 以第一个六边形为参照物
let secondHexagonCenterX = firstHexagonCenterX + config.sideLength * Math.sin(angle) * 2; let secondHexagonCenterY = firstHexagonCenterY;
绘制出的两个六边形
改了一下宽度, 方便观察
最后一个六边形的最右侧的边, 不能超过画板
let hexagonPaths = []; let count = 1; while (1) { let nextHexagonCenterX = firstHexagonCenterX + config.sideLength * Math.sin(angle) * 2 * count; if (nextHexagonCenterX + config.sideLength * Math.sin(angle) + config.paintConfig.width > canvasWidth) { break; } let nextHexagonCenterY = firstHexagonCenterY; let nextHexagon = new Hexagon(nextHexagonCenterX, nextHexagonCenterY, config.sideLength); let nextHexagonPath = nextHexagon.getPath(); hexagonPaths.push(nextHexagonPath); count++; }
for (let i = 0; i < hexagonPaths.length; i++) { canvas.drawPath(hexagonPaths[i], paint); }
计算第二排第一个六边形的中心点
let nextRowFirstHexagonCenterX = firstHexagonCenterX + config.sideLength * Math.sin(angle); let nextRowFirstHexagonCenterY = firstHexagonCenterY + config.sideLength * Math.cos(angle) + config.sideLength; let nextRowFirstHexagon = new Hexagon(nextRowFirstHexagonCenterX, nextRowFirstHexagonCenterY, config.sideLength); let nextRowFirstHexagonPath = nextRowFirstHexagon.getPath();
与第一排同理, 画第二排
很明显, 我们要使用循环, 横着要循环, 竖着也要循环, 那么这个循环怎么写呢?
我们一组一组的计算六边形中心点, 一组有两排六边形:
下一组, 往下移动 3 个边长,
因为第一组, 我们已经检查过画板右侧,
所以, 接下来的计算, 只需要考虑画板底部, 不需要判断画板右侧了;
我们改变的只有纵坐标
let firstRow = []; let secondRow = []; let firstGroup = [firstRow, secondRow]; let firstRowCount = 0; while (1) { let centerX = firstHexagonCenterX + firstRowCount * config.sideLength * 2 * Math.sin(angle); if (centerX > canvasWidth - config.sideLength * Math.sin(angle) - config.paintConfig.width) { break; } let centerY = firstHexagonCenterY; firstRow.push(new Hexagon(centerX, centerY, config.sideLength)); firstRowCount++; } /* -------------------------------------------------------------------------- */ let secondRowCount = 0; while (1) { let centerX = firstHexagonCenterX + secondRowCount * config.sideLength * 2 * Math.sin(angle) + config.sideLength * Math.sin(angle); if (centerX > canvasWidth - config.sideLength * Math.sin(angle) - config.paintConfig.width) { break; } let centerY = firstHexagonCenterY + config.sideLength + config.sideLength * Math.cos(angle); secondRow.push(new Hexagon(centerX, centerY, config.sideLength)); secondRowCount++; } hexagons = firstGroup.flat(); let hexagonPaths = hexagons.map((hexagon) => hexagon.getPath()); /* -------------------------------------------------------------------------- */ canvasView.on("draw", (canvas) => { canvas.drawColor(canvasBgColor); for (let i = 0; i < hexagonPaths.length; i++) { canvas.drawPath(hexagonPaths[i], paint); } });
这是一个二维数组, 我们用flat抹平成一维数组
y坐标依次增长3个边长即可
先计算第一排, 再计算第二排
let row = 1; while (1) { let nextGroupFirstRow = []; let nextGroupSecondRow = []; let nextGroup = [nextGroupFirstRow, nextGroupSecondRow]; let firstGroupFirstRow = firstGroup[0]; let firstGroupFirstHexagon = firstGroupFirstRow[0]; if (firstGroupFirstHexagon.centerY + config.sideLength * 3 * row > canvasHeight) { break; } nextGroupFirstRow = firstGroupFirstRow.map((hexagon) => { let centerX = hexagon.centerX; let centerY = hexagon.centerY + config.sideLength * 3 * row; nextGroupFirstRow.push(new Hexagon(centerX, centerY, config.sideLength)); }); let firstGroupSecondRow = firstGroup[1]; let firstGroupSecondHexagon = firstGroupSecondRow[0]; if (firstGroupSecondHexagon.centerY + config.sideLength * 3 * row > canvasHeight) { break; } nextGroupSecondRow = firstGroupSecondRow.map((hexagon) => { let centerX = hexagon.centerX; let centerY = hexagon.centerY + config.sideLength * 3 * row; nextGroupSecondRow.push(new Hexagon(centerX, centerY, config.sideLength)); }); groups.push(nextGroup); row++; } hexagons = groups.flat(2); let hexagonPaths = hexagons.map((hexagon) => hexagon.getPath());
设备: 小米11pro
Android版本: 12
Autojs版本: 9.3.11
名人名言
思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问 ---
部分内容来自网络 本教程仅用于学习, 禁止用于其他用途
以上就是autojs画六边形实现示例详解的详细内容,更多关于autojs绘画实现六边形的资料请关注脚本之家其它相关文章!
编程 | 2023-02-24 21:36
编程 | 2023-02-21 12:51
编程 | 2023-02-21 12:47
编程 | 2023-02-21 00:15
编程 | 2023-02-21 00:08
编程 | 2023-02-20 21:46
编程 | 2023-02-20 21:42
编程 | 2023-02-20 21:36
编程 | 2023-02-20 21:32
编程 | 2023-02-20 18:12
网友评论