Skip to content

Commit

Permalink
数学-向量表示
Browse files Browse the repository at this point in the history
  • Loading branch information
tedjmzhang committed Jun 24, 2024
1 parent 7199051 commit c6d6124
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 6 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"ajv": "^8.16.0",
"chalk": "^5.3.0",
"commander": "^12.1.0",
"enhanced-resolve": "^5.17.0",
"gogocode": "^1.0.55",
"handlebars": "^4.7.8",
"inquirer": "^8.2.6",
Expand Down
26 changes: 26 additions & 0 deletions src/frontend/library/enhanced-resolve/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const resolve = require("enhanced-resolve");
const path =require('path')

// resolve("/some/path/to/folder", "module/dir", (err, result) => {
// result; // === "/some/path/node_modules/module/dir/index.js"
// });
console.log(__dirname); // /Users/zhangjiaming/code/learn/blog/src/frontend/library/enhanced-resolve
let res = resolve.sync(__dirname, "asap"); // /Users/zhangjiaming/code/learn/blog/node_modules/asap/asap.js
let res2 = resolve.sync(__dirname, "esbuild"); // /Users/zhangjiaming/code/learn/blog/node_modules/esbuild/lib/main.js
console.log(res);
console.log(res2);

const myResolve = resolve.create({
// or resolve.create.sync
extensions: [".ts", ".js"],
alias: {
"@": __dirname
}
// see more options below
});

let res3 = myResolve('/', "@/index", (err, result) => {
if(err) throw err
console.log('res3', result);
});

14 changes: 14 additions & 0 deletions src/frontend/library/enhanced-resolve/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# enhanced-resolve 获取绝对路径,用来处理模块引用的
[通过可以设置各种配置,例如别名,根路径等](https://www.npmjs.com/package/enhanced-resolve)

```
// webpack使用该库处理成xxx/node_modules/moduleA/xxx, 后面的xxx是该库的package.json里面的main定义的
import moduleA from 'moduleA'
// 处理相对路径 xxx/moduleB/xxx.[ext]
import moduleB from './moduleB'
```

50 changes: 45 additions & 5 deletions src/frontend/library/tapable/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
let { SyncHook, AsyncSeriesHook } = require('tapable');
let { SyncHook, AsyncSeriesHook, SyncBailHook } = require('tapable');

let syncHook = new SyncHook(['param1', 'param2']);
let asyncSeriesHook = new AsyncSeriesHook(['param1', 'param2']);
let syncBailHook = new SyncBailHook(['param1', 'param2']);

syncHook.tap({
name: '1 third hook',
Expand Down Expand Up @@ -62,7 +63,7 @@ asyncSeriesHook.tapAsync({
asyncSeriesHook.tapPromise({
name: '3 hook',
stage: 3
}, async function (param, other, callback) {
}, async function (param, other, callback) {
fetch() // 异步获取些东西
console.log('3 hook', param, other);
callback();
Expand All @@ -77,12 +78,29 @@ asyncSeriesHook.tapPromise({
})

// console.log(asyncSeriesHook.callAsync.toString());
asyncSeriesHook.promise(1111, 2222).then((res) => {
console.log('ok', res);
}).catch(err => console.log('something wrong', err))
// asyncSeriesHook.promise(1111, 2222).then((res) => {
// console.log('ok', res);
// }).catch(err => console.log('something wrong', err))



syncBailHook.tap({
name: '1 third hook',
stage: 1
}, function (param, other) {
console.log('first param2', param, other);
return 11
})
syncBailHook.tap({
name: 'first hook',
stage: 11
}, function (param, other) {
console.log('first param', param, other)
})
let res = syncBailHook.call('bail', 'bail2');
console.log(res)





Expand Down Expand Up @@ -211,4 +229,26 @@ function anonymous3(param1, param2
_sync = false;
}));

}

// bail的hook需要返回undefined才能继续执行,不然直接用返回值结束了
function syncBailHookFn(param1, param2
) {
"use strict";
var _context;
var _x = this._x;
var _fn0 = _x[0];
var _result0 = _fn0(param1, param2);
if (_result0 !== undefined) {
return _result0;
;
} else {
var _fn1 = _x[1];
var _result1 = _fn1(param1, param2);
if (_result1 !== undefined) {
return _result1;
;
} else {
}
}
}
17 changes: 16 additions & 1 deletion src/frontend/library/webpack/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,19 @@ webpack内部原生支持转换javascript(使用acorn作为parser),

## 开冲

1.
1. Compiler实例
保存传入的参数options,调用compiler.run调用hooks之后,执行compiler.compile生成Compilation实例,存在this.compilation中

2. Compilation实例
调用compilation


流程

1. 调用webpack()会生成Compiler实例,生成的时候会添加各种hooks(JavascriptModulesPlugin,CssModulesPlugin,EntryOptionPlugin...),
调用compiler.run()会触发compiler的hooks,然后调用compiler.create()生成compilation实例。
2. compiler.hooks.make.callAsync()调用make的hooks,包含了EntryOptionPlugin,参数为compilation
3. compilation实例添加属性compilation.factorizeQueue=new AsyncQueue()。
AsyncQueue:自身包含hooks,例如asyncQueue.hooks.beforeAdd。调用了asyncQueue.add之后会在加上setImmediate(syncQueue._root._ensureProcessing)(事件循环末尾),会遍历syncQueue._children里面的所有child,
调用child._startProcessing(entry)
4. setImmediate
45 changes: 45 additions & 0 deletions src/数学/向量/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 公式: 向量a.向量b = |a||b|cosAngle。
// 公式:向量a.向量b = (a.X * b.X) + (b.Y * b.Y)
// 结合两者可以求出cosAngle,然后求出比例ratio

/**
* @typedef Point
* @property {number} latitude
* @property {number} longitude
*/

/**
* @desc: 求其他点在起终点对应直线的投影(ps: 常用地理坐标系是二维坐标系)
* @param {Point} startPoint 起点
* @param {Point} endPoint 终点
* @param {Point} point 其他点
*/
function getAbsorbPoint(startPoint, endPoint, point) {
const {
longitude: sX,
latitude: sY
} = startPoint
const {
longitude: eX,
latitude: eY
} = endPoint
const {
longitude: pX,
latitude: pY
} = point

let so = [sX, sY]; // 向量so,从原点o指向startPoint
let eo = [eX, eY]; // 向量eo,从原点o指向endPoint
let po = [pX, pY]; // 向量po,从原点o指向point
let es = [eo[0] - so[0], eo[1] - so[1]]; // 向量es,由向量eo - 向量so
let ps = [po[0] - so[0], po[1] - so[1]]; // 向量ps,由向量po - 向量so
let esMang = Math.sqrt(Math.pow(es[0], 2) + Math.pow(es[1], 2)) // 向量es的模
let psMang = Math.sqrt(Math.pow(ps[0], 2) + Math.pow(ps[1], 2)) // 向量ps的模

let cosAngle = (es[0] * ps[0] + es[1] * ps[1]) / esMang / psMang;
let ratio = psMang * cosAngle / esMang; // 其实就是(es[0] * ps[0] + es[1] * ps[1]) / Math.pow(esMang, 2)
return {
longitude: sX + ratio * es[0],
latitude: sY + ratio * es[1]
}
}

0 comments on commit c6d6124

Please sign in to comment.