Best JavaScript code snippet using wpt
RectArea.js
Source:RectArea.js
1/* eslint-disable */2import Point from "./Point";3import LineSegment from "../curve/LineSegment";4class RectArea {5 constructor(start,{ width, height }) {6 this.start = start;7 this.size = { width, height };8 }9 getStart() {10 return new Point(this.start.x, this.start.y)11 }12 getSize() {13 return { ...this.size };14 }15 applyMatrix(matrix) {16 let points = this.getPoints();17 let _points = points.map(p => p.applyMatrix(matrix));18 return RectArea.fromPoints(_points);19 }20 getRectIntersection(rectArea) {21 let points = this.getPoints();22 let points1 = points.filter(p => rectArea.containsPoint(p));23 points = rectArea.getPoints();24 let points2 = points.filter(p => this.containsPoint(p));25 let crossPoints = [];26 let line1s = this.getBoundaries();27 let line2s = rectArea.getBoundaries();28 line1s.forEach(l => {29 line2s.forEach(_l => {30 let p = l.getCrossPoints(_l);31 if(p && p[0]) {32 crossPoints.push(p[0]);33 }34 });35 });36 return RectArea.fromPoints([...points1,...points2,...crossPoints]);37 }38 getBoundaries() {39 let [p1,p2,p3,p4] = this.getPoints();40 let line1 = new LineSegment(p1,p2);41 let line2 = new LineSegment(p2,p3);42 let line3 = new LineSegment(p3,p4);43 let line4 = new LineSegment(p4,p1);44 return [line1,line2,line3,line4,];45 }46 getPoints() {47 let {48 start,49 size,50 } = this;51 let p1 = new Point(start.x,start.y);52 let p2 = new Point(start.x + size.width,start.y);53 let p3 = new Point(start.x + size.width,start.y + size.height);54 let p4 = new Point(start.x,start.y+ size.height);55 return [p1,p2,p3,p4];56 }57 checkImpactCircle(center,radius) {58 if (this.containsPoint(center)) {59 return true;60 }61 return false;62 }63 checkImpact(rectArea) {64 let points = rectArea.getPoints();65 for(let i=0;i<points.length;i++) {66 if(this.containsPoint(points[i])) {67 return true;68 }69 }70 points = this.getPoints();71 for(let i=0;i<points.length;i++) {72 if(rectArea.containsPoint(points[i])) {73 return true;74 }75 }76 let lines1 = this.getBoundaries();77 let lines2 = rectArea.getBoundaries();78 for(let i=0;i<lines1.length;i++) {79 for(let j=0;j<lines2.length;j++) {80 if(lines1[i].getCrossPoint(lines2[j])) {81 return true;82 }83 }84 }85 return false;86 }87 containsRectArea(rectArea) {88 let points = rectArea.getPoints();89 for(let i=0;i<points.length;i++) {90 if(!this.containsPoint(points[i])) {91 return false;92 }93 }94 return true;95 }96 containsPoint({x,y},inBoundary = true) {97 let {98 start,99 size,100 } = this;101 let deltaX = x - start.x;102 let deltaY = y - start.y;103 if(inBoundary) {104 return deltaX >= 0 && deltaY >= 0 && deltaX <= size.width && deltaY <= size.height;105 }else {106 return deltaX > 0 && deltaY > 0 && deltaX < size.width && deltaY < size.height;107 }108 }109}110RectArea.fromPoints = function(points) {111 if (points && points.length) {112 const _point = {113 minx: Number.MAX_SAFE_INTEGER,114 miny: Number.MAX_SAFE_INTEGER,115 maxx: Number.MIN_SAFE_INTEGER,116 maxy: Number.MIN_SAFE_INTEGER,117 };118 points.forEach((p) => {119 _point.maxx = Math.max(_point.maxx, p.x);120 _point.maxy = Math.max(_point.maxy, p.y);121 _point.minx = Math.min(_point.minx, p.x);122 _point.miny = Math.min(_point.miny, p.y);123 });124 const {125 minx, miny, maxx, maxy,126 } = _point;127 const width = maxx - minx;128 const height = maxy - miny;129 let start = new Point(minx,miny);130 let size = {width,height};131 return new RectArea(start,size);132 }133}...
loadCaptureRectImage.ts
Source:loadCaptureRectImage.ts
1/**2 * 计ç®ææåºå3 */4export function getCaptureRect(rect, type, isRealy) {5 let rectArea = rect.split(',');6 if (rectArea.length !== 4) {7 throw Error('åºåæ误 å®ä¾ï¼string = "x,y,w,h"! ');8 }9 if (isRealy) {10 return rectArea;11 }12 let x, y, w, h;13 switch (type) {14 case 'body':15 x = rectArea[0] - rectArea[2] * 0.2;16 y = rectArea[1] - rectArea[3] * 0.2;17 w = rectArea[2] * 1.4;18 h = rectArea[3] * 1.4;19 break;20 case 'nonVehicle':21 case 'vehicle':22 case 'plate':23 case 'vehiclesPassengers':24 x = rectArea[0];25 y = rectArea[1];26 w = rectArea[2];27 h = rectArea[3];28 break;29 case 'nonvehiclesPassengers':30 x = rectArea[0] - rectArea[2] * 0.0000001 * 0.8;31 y = rectArea[1] - rectArea[3] * 0.01;32 w = rectArea[2] * 0.8;33 h = rectArea[3] * 0.4;34 break;35 default:36 // face37 x = rectArea[0] - rectArea[2] * 0.8;38 y = rectArea[1] - rectArea[3] * 1.5;39 w = rectArea[2] * 2.6;40 h = rectArea[3] * 3.2;41 break;42 }43 return [x, y, w, h];44}45export async function loadCaptureRectImage(image, rect, imageType) {46 if (!image) {47 throw Error('image is HTMLImgElement');48 }49 let [x, y, w, h] = getCaptureRect(rect, imageType);50 let canvas = document.createElement('canvas');51 let ctx = canvas.getContext('2d');52 canvas.width = image.width;53 canvas.height = image.height;54 ctx.drawImage(image, 0, 0, canvas.width, canvas.height);55 ctx.beginPath();56 ctx.strokeStyle = 'red';57 ctx.lineWidth = 3;58 ctx.strokeRect(x, y, w, h);59 let url = canvas.toDataURL();60 setTimeout(() => {61 canvas.remove();62 canvas = null;63 ctx = null;64 }, 500);65 return url;66}67export function getRectImagePath(68 sourceImage,69 cuurentImage,70 rect,71 options = { scale: 1, rotate: 0, x: 0, y: 0 }72) {73 const { scale, rotate } = options;74 const [x, y, w, h] = rect;75 const { width, height } = sourceImage;76 const sourceSize = {77 width,78 height,79 cwidth: cuurentImage.width,80 cheight: cuurentImage.height,81 wScale: width / cuurentImage.width,82 hScale: height / cuurentImage.height,83 };84 const area = [85 x * sourceSize.wScale,86 y * sourceSize.hScale,87 w * sourceSize.wScale,88 h * sourceSize.hScale,89 ];90 let fullCanvas = document.createElement('canvas');91 let fullCtx = fullCanvas.getContext('2d');92 fullCanvas.width = width;93 fullCanvas.height = height;94 fullCtx.save();95 // fullCtx.transform;96 // document.body.appendChild(fullCanvas);97 fullCtx.setTransform(98 scale,99 0,100 0,101 scale,102 options.x * sourceSize.wScale - ((scale - 1) * width) / 2,103 options.y * sourceSize.hScale - ((scale - 1) * height) / 2104 );105 fullCtx.translate(width / 2, height / 2);106 fullCtx.rotate((rotate * Math.PI) / 180);107 fullCtx.drawImage(sourceImage, -width / 2, -height / 2, width, height);108 fullCtx.restore();109 let imageData = fullCtx.getImageData(...area);110 let tempCanvas = document.createElement('canvas');111 let tempCtx = tempCanvas.getContext('2d');112 tempCanvas.width = area[2];113 tempCanvas.height = area[3];114 // document.body.appendChild(tempCanvas);115 tempCtx.putImageData(imageData, 0, 0);116 const dataUrl = tempCanvas.toDataURL();117 setTimeout(() => {118 fullCanvas.remove();119 tempCanvas.remove();120 fullCanvas = null;121 tempCanvas = null;122 tempCtx = null;123 fullCtx = null;124 imageData = null;125 }, 100);126 return dataUrl;127}128export function cloneImageNode(image) {129 if (!image) {130 return Promise.resolve();131 }132 let newImage = image.cloneNode();133 return new Promise((resolve, reject) => {134 newImage.addEventListener(135 'load',136 function () {137 resolve(this);138 },139 false140 );141 newImage.addEventListener(142 'error',143 function (e) {144 reject(e);145 },146 false147 );148 });...
loadCaptureRectImage.js
Source:loadCaptureRectImage.js
1/**2 * 计ç®ææåºå3 */4export function getCaptureRect(rect, type, isRealy) {5 let rectArea = rect.split(',');6 if (rectArea.length !== 4) {7 throw Error('åºåæ误 å®ä¾ï¼string = "x,y,w,h"! ');8 }9 if (isRealy) {10 return rectArea;11 }12 let x, y, w, h;13 switch (type) {14 case 'body':15 x = rectArea[0] - rectArea[2] * 0.2;16 y = rectArea[1] - rectArea[3] * 0.2;17 w = rectArea[2] * 1.4;18 h = rectArea[3] * 1.4;19 break;20 case 'nonVehicle':21 case 'vehicle':22 case 'vehiclesPassengers':23 x = rectArea[0];24 y = rectArea[1];25 w = rectArea[2];26 h = rectArea[3];27 break;28 case 'nonvehiclesPassengers':29 x = rectArea[0] - rectArea[2] * 0.0000001 * 0.8;30 y = rectArea[1] - rectArea[3] * 0.01;31 w = rectArea[2] * 0.8;32 h = rectArea[3] * 0.4;33 break;34 default:35 // face36 x = rectArea[0] - rectArea[2] * 0.8;37 y = rectArea[1] - rectArea[3] * 1.5;38 w = rectArea[2] * 2.6;39 h = rectArea[3] * 3.2;40 break;41 }42 return [x, y, w, h];43}44export async function loadCaptureRectImage(image, rect, imageType) {45 if (!image) {46 throw Error('image is HTMLImgElement');47 }48 let [x, y, w, h] = getCaptureRect(rect, imageType);49 let canvas = document.createElement('canvas');50 let ctx = canvas.getContext('2d');51 canvas.width = image.width;52 canvas.height = image.height;53 ctx.drawImage(image, 0, 0, canvas.width, canvas.height);54 ctx.beginPath();55 ctx.strokeStyle = 'red';56 ctx.lineWidth = 3;57 ctx.strokeRect(x, y, w, h);58 let url = canvas.toDataURL();59 setTimeout(() => {60 canvas.remove();61 canvas = null;62 ctx = null;63 }, 500);64 return url;65}66export function getRectImagePath(67 sourceImage,68 cuurentImage,69 rect,70 options = { scale: 1, rotate: 0, x: 0, y: 0 },71) {72 const { scale, rotate } = options;73 const [x, y, w, h] = rect;74 const { width, height } = sourceImage;75 const sourceSize = {76 width,77 height,78 cwidth: cuurentImage.width,79 cheight: cuurentImage.height,80 wScale: width / cuurentImage.width,81 hScale: height / cuurentImage.height,82 };83 const area = [84 x * sourceSize.wScale,85 y * sourceSize.hScale,86 w * sourceSize.wScale,87 h * sourceSize.hScale,88 ];89 let fullCanvas = document.createElement('canvas');90 let fullCtx = fullCanvas.getContext('2d');91 fullCanvas.width = width;92 fullCanvas.height = height;93 fullCtx.save();94 // fullCtx.transform;95 // document.body.appendChild(fullCanvas);96 fullCtx.setTransform(97 scale,98 0,99 0,100 scale,101 options.x * sourceSize.wScale - ((scale - 1) * width) / 2,102 options.y * sourceSize.hScale - ((scale - 1) * height) / 2,103 );104 fullCtx.translate(width / 2, height / 2);105 fullCtx.rotate((rotate * Math.PI) / 180);106 fullCtx.drawImage(sourceImage, -width / 2, -height / 2, width, height);107 fullCtx.restore();108 let imageData = fullCtx.getImageData(...area);109 let tempCanvas = document.createElement('canvas');110 let tempCtx = tempCanvas.getContext('2d');111 tempCanvas.width = area[2];112 tempCanvas.height = area[3];113 // document.body.appendChild(tempCanvas);114 tempCtx.putImageData(imageData, 0, 0);115 const dataUrl = tempCanvas.toDataURL();116 setTimeout(() => {117 fullCanvas.remove();118 tempCanvas.remove();119 fullCanvas = null;120 tempCanvas = null;121 tempCtx = null;122 fullCtx = null;123 imageData = null;124 }, 100);125 return dataUrl;126}127export function cloneImageNode(image) {128 if (!image) {129 return Promise.resolve();130 }131 let newImage = image.cloneNode();132 return new Promise((resolve, reject) => {133 newImage.addEventListener(134 'load',135 function() {136 resolve(this);137 },138 false,139 );140 newImage.addEventListener(141 'error',142 function(e) {143 reject(e);144 },145 false,146 );147 });...
Using AI Code Generation
1var rect = require('./wpt.js');2function solveRect(l,b) {3 console.log("Solving for rectangle with l = " + l + " and b = " + b);4 if (l <= 0 || b <= 0) {5 console.log("Rectangle dimensions should be greater than zero: l = " + l + ", and b = " + b);6 }7 else {8 console.log("The area of the rectangle of dimensions length = " + l + " and breadth = " + b + " is " + rect.area(l,b));9 console.log("The perimeter of the rectangle of dimensions length = " + l + " and breadth = " + b + " is " + rect.perimeter(l,b));10 }11}12solveRect(2,4);13solveRect(3,5);14solveRect(0,5);15solveRect(-3,5);16exports.area = function(x,y) {17 return (x*y);18}19exports.perimeter = function(x,y) {20 return (2*(x+y));21}22{23 "scripts": {24 },25 "dependencies": {},26 "devDependencies": {}27}
Using AI Code Generation
1var rect = require('./wpt.js');2function solveRect(l,b){3 console.log("Solving for rectangle with l = " + l + " and b = " + b);4 if(l <= 0 || b <= 0){5 console.log("Rectangle dimensions should be greater than zero: l = " + l + ", and b = " + b);6 }7 else{8 console.log("The area of the rectangle of dimensions length = " + l + " and breadth = " + b + " is " + rect.area(l,b));9 console.log("The perimeter of the rectangle of dimensions length = " + l + " and breadth = " + b + " is " + rect.perimeter(l,b));10 }11}12solveRect(2,4);13solveRect(3,5);14solveRect(-3,5);
Using AI Code Generation
1var rect = require('./wpt.js');2function solveRect(l,b){3 console.log("Solving for rectangle with l = " + l + " and b = " + b);4 if(l <= 0 || b <= 0){5 console.log("Rectangle dimensions should be greater than zero: l = " + l + ", and b = " + b);6 }7 else{8 console.log("The area of the rectangle of dimensions length = " + l + " and breadth = " + b + " is " + rect.area(l,b));9 console.log("The perimeter of the rectangle of dimensions length = " + l + " and breadth = " + b + " is " + rect.perimeter(l,b));10 }11}12solveRect(2,4);13solveRect(3,5);14solveRect(0,5);15solveRect(-3,5);16exports.perimeter = (x,y) => (2*(x+y));17exports.area = (x,y) => (x*y);18var http = require('http');19Module Description assert Provides functions to test invariants assert.equal(value1, value2, message); assert.notEqual(value1, value2, message);
Using AI Code Generation
1var wpt = require('./wpt.js');2console.log('Area of rectangle = ' + wpt.rectArea(10,20));3console.log('Area of circle = ' + wpt.circleArea(10));4console.log('Area of square = ' + wpt.squareArea(10));5console.log('Area of triangle = ' + wpt.triangleArea(10,20));6console.log('Perimeter of rectangle = ' + wpt.rectanglePerimeter(10,20));7console.log('Perimeter of circle = ' + wpt.circlePerimeter(10));8console.log('Perimeter of square = ' + wpt.squarePerimeter(10));9console.log('Perimeter of triangle = ' + wpt.trianglePerimeter(10,20,30));10console.log('Area of cube = ' + wpt.cubeArea(10));11console.log('Area of sphere = ' + wpt.sphereArea(10));12console.log('Volume of cube = ' + wpt.cubeVolume(10));13console.log('Volume of sphere = ' + wpt.sphereVolume(10));14console.log('Volume of cylinder = ' + wpt.cylinderVolume(10,20));15console.log('Volume of cone = ' + wpt.coneVolume(10,20));16console.log('Surface area of cone = ' + wpt.coneSurfaceArea(10,20));17console.log('Surface area of cylinder = ' + wpt.cylinderSurfaceArea(10,20));
Using AI Code Generation
1var wpt = require('wpt');2var area = wpt.rectArea(10, 20);3console.log(area);4exports.rectArea = function (w, h) {5 return w * h;6};7You can also use the require() method to include core Node.js modules. For example, to include the HTTP module, you can use the following code:8var http = require('http');9The following example shows how you can use the require() method to include a module from a different folder:10var wpt = require('./lib/wpt');11var area = wpt.rectArea(10, 20);12console.log(area);13exports.rectArea = function (w, h) {14 return w * h;15};16You can also use the require() method to include core Node.js modules. For example, to include the HTTP module, you can use the following code:17var http = require('http');18The following example shows how you can use the require() method to include a module from a different folder:19var wpt = require('./lib/wpt');20var area = wpt.rectArea(10, 20);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!