anytriangle.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <template>
  2. <view class="math-container">
  3. <view class="header">
  4. <u-image width="100%" height="300rpx" :src="mathImgs[name].big" mode="aspectFit" @click="handleBigImage"></u-image>
  5. </view>
  6. <view class="main dir-top-wrap cross-center">
  7. <view class="form">
  8. <u-form-item label="边长a" label-width="160">
  9. <u-input type="number" v-model="formData.lengtha" :placeholder="lengthAInput.placeholder" :disabled="lengthAInput.disabled"/>
  10. </u-form-item>
  11. <u-form-item label="边长b" label-width="160">
  12. <u-input type="number" v-model="formData.lengthb" :placeholder="lengthBInput.placeholder" :disabled="lengthBInput.disabled"/>
  13. </u-form-item>
  14. <u-form-item label="边长c" label-width="160">
  15. <u-input type="number" v-model="formData.lengthc" :placeholder="lengthCInput.placeholder" :disabled="lengthCInput.disabled"/>
  16. </u-form-item>
  17. <u-form-item label="角度A" label-width="160">
  18. <u-input type="number" v-model="formData.anglea" :placeholder="angleAInput.placeholder" :disabled="angleAInput.disabled"/>
  19. </u-form-item>
  20. <u-form-item label="角度B" label-width="160">
  21. <u-input type="number" v-model="formData.angleb" :placeholder="angleBInput.placeholder" :disabled="angleBInput.disabled"/>
  22. </u-form-item>
  23. <u-form-item label="角度C" label-width="160">
  24. <u-input type="number" v-model="formData.anglec" :placeholder="angleCInput.placeholder" :disabled="angleCInput.disabled"/>
  25. </u-form-item>
  26. </view>
  27. <view class="btn-group main-left cross-center">
  28. <u-button :ripple="true" @click="handelCalc" :custom-style="{backgroundColor: $u.color['mainBgColor'],color:'#ffffff',width: '260rpx',marginRight:'30rpx'}">计算</u-button>
  29. <u-button :ripple="true" @click="handleClear">清空</u-button>
  30. </view>
  31. </view>
  32. <div class="footer">
  33. <view class="result dir-top-wrap cross-center" v-if="showResult">
  34. <view v-for="item in rules" v-if="item.value && item.show">
  35. <text>{{item.name}}=</text>{{item.value}}{{item.unit}}
  36. </view>
  37. </view>
  38. </div>
  39. </view>
  40. </template>
  41. <script>
  42. import mathImgs from "@/core/math-imgs"
  43. // 万能公式
  44. export default {
  45. data() {
  46. return {
  47. name: 'anytriangle',
  48. mathImgs: mathImgs,
  49. formData: {
  50. lengtha: '',
  51. lengthb: '',
  52. lengthc: '',
  53. anglea: '',
  54. angleb: '',
  55. anglec: '',
  56. },
  57. // 用来验证 输入
  58. rules: {
  59. lengtha: {name:'a', value:'',unit:'cm',show: true},
  60. lengthb:{name:'b', value:'',unit:'cm',show: true},
  61. lengthc: {name:'c', value:'',unit:'cm',show: true},
  62. anglea:{name:'角度A', value:'',unit:'°',show: true},
  63. angleb:{name:'角度B', value:'',unit:'°',show: true},
  64. anglec:{name:'角度C', value:'',unit:'°',show: true},
  65. }
  66. }
  67. },
  68. methods: {
  69. handleBigImage(){
  70. uni.previewImage({
  71. urls: [mathImgs[this.name].big],
  72. });
  73. },
  74. handelCalc(){
  75. this.initRules();
  76. let validate = [];
  77. for (const itemKey in this.formData) {
  78. validate.push(this.formData[itemKey]);
  79. }
  80. let lengtha = 0;
  81. let lengthb = 0;
  82. let lengthc = 0;
  83. let anglea = 0;
  84. let angleb = 0;
  85. let anglec = 0;
  86. lengtha = parseFloat( this.formData.lengtha)?parseFloat(this.formData.lengtha):0;
  87. lengthb = parseFloat(this.formData.lengthb)?parseFloat(this.formData.lengthb):0;
  88. lengthc = parseFloat(this.formData.lengthc)?parseFloat(this.formData.lengthc):0;
  89. anglea = parseFloat(this.formData.anglea)?parseFloat(this.formData.anglea):0;
  90. angleb = parseFloat(this.formData.angleb)?parseFloat(this.formData.angleb):0;
  91. anglec = parseFloat(this.formData.anglec)?parseFloat(this.formData.anglec):0;
  92. if(anglea && anglea > 180){
  93. this.$u.toast('角度A不能大于180');
  94. return;
  95. }
  96. if(angleb && angleb > 180){
  97. this.$u.toast('角度B不能大于180');
  98. return;
  99. }
  100. if(anglec && anglec > 180){
  101. this.$u.toast('角度C不能大于180');
  102. return;
  103. }
  104. if((anglea + angleb + anglec) > 180){
  105. this.$u.toast('内角之和不能大于180');
  106. return;
  107. }
  108. if(!this.$util.checkArrayNotNullNumber(validate,3)){
  109. this.$u.toast('至少输入三个参数');
  110. return;
  111. }
  112. /**1、 已知a、b、c
  113. * cosA=(b²+c²-a²)÷(2bc)
  114. * cosB=(a²+c²-b²)÷(2ac)
  115. * cosC =(a²+b²-c²)÷(2ab)
  116. */
  117. if(lengtha && lengthb && lengthc){
  118. if(!this.$util.checkTriangle(lengtha, lengthb, lengthc)){
  119. this.$u.toast('两边之后要大于第三边');
  120. return;
  121. }
  122. anglea = this.$util.acos((Math.pow(lengthb,2) + Math.pow(lengthc,2) - Math.pow(lengtha,2)) / (2 * lengthb * lengthc))
  123. angleb = this.$util.acos((Math.pow(lengtha,2) + Math.pow(lengthc,2) - Math.pow(lengthb,2)) / (2 * lengtha * lengthc))
  124. anglec = this.$util.acos((Math.pow(lengtha,2) + Math.pow(lengthb,2) - Math.pow(lengthc,2)) / (2 * lengtha * lengthb))
  125. }
  126. /** 2、 已知a、b、A(a、b、B;a、c、A;a、c、C;b、c、B;b、c、C):
  127. * sinB=(b×sinA)÷a
  128. * c:a²=b²+c²-2bccosA
  129. * sinC=(c×sinA)÷a
  130. *
  131. */
  132. else if(lengtha && lengthb){
  133. //let c1 = ((2 * b * Math.cos(A)) + Math.sqrt(Math.pow(2 * b * Math.cos(A), 2) - 4 * (Math.pow(b, 2) - Math.pow(a, 2)))) / 2
  134. if(anglea){
  135. angleb = this.$util.asin((lengthb * this.$util.sin(anglea)) / lengtha)
  136. lengthc = ((2 * lengthb * this.$util.cos(anglea)) + Math.sqrt(Math.pow(2 * lengthb * this.$util.cos(anglea), 2) - 4 * (Math.pow(lengthb, 2) - Math.pow(lengtha, 2)))) / 2
  137. if(!this.$util.checkTriangle(lengtha, lengthb, lengthc)){
  138. lengthc = ((2 * lengthb * this.$util.cos(anglea)) - Math.sqrt(Math.pow(2 * lengthb * this.$util.cos(anglea), 2) - 4 * (Math.pow(lengthb, 2) - Math.pow(lengtha, 2)))) / 2
  139. }
  140. anglec = 180 - angleb - anglea
  141. }else if(angleb){
  142. anglea = this.$util.asin((lengtha * this.$util.sin(angleb)) / lengthb)
  143. lengthc = ((2 * lengtha * this.$util.cos(angleb)) + Math.sqrt(Math.pow(2 * lengtha * this.$util.cos(angleb), 2) - 4 * (Math.pow(lengtha, 2) - Math.pow(lengthb, 2)))) / 2
  144. if(!this.$util.checkTriangle(lengtha, lengthb, lengthc)){
  145. lengthc = ((2 * lengtha * this.$util.cos(angleb)) - Math.sqrt(Math.pow(2 * lengtha * this.$util.cos(angleb), 2) - 4 * (Math.pow(lengtha, 2) - Math.pow(lengthb, 2)))) / 2
  146. }
  147. anglec = 180 - angleb - anglea
  148. }else if(anglec){
  149. // sinA=(a×sinC)÷c
  150. // c²=a²+b²-2ab cosC
  151. lengthc = Math.sqrt(Math.pow(lengtha, 2) + Math.pow(lengthb, 2) - 2 * lengtha * lengthb * this.$util.cos(anglec))
  152. anglea = this.$util.asin((lengtha * this.$util.sin(anglec)) / lengthc)
  153. angleb = 180 - anglec - anglea
  154. }
  155. }else if(lengtha && lengthc){
  156. if(anglea){
  157. anglec = this.$util.asin((lengthc * this.$util.sin(anglea)) / lengtha)
  158. lengthb = ((2 * lengthc * this.$util.cos(anglea)) + Math.sqrt(Math.pow(2 * lengthc * this.$util.cos(anglea), 2) - 4 * (Math.pow(lengthc, 2) - Math.pow(lengtha, 2)))) / 2
  159. if(!this.$util.checkTriangle(lengtha, lengthb, lengthc)){
  160. lengthb = ((2 * lengthc * this.$util.cos(anglea)) - Math.sqrt(Math.pow(2 * lengthc * this.$util.cos(anglea), 2) - 4 * (Math.pow(lengthc, 2) - Math.pow(lengtha, 2)))) / 2
  161. }
  162. angleb = 180 - anglec - anglea
  163. }else if(angleb){
  164. // sinA=(a×sinB)÷b
  165. // b²=a²+c²-2ac cosC
  166. lengthb = Math.sqrt(Math.pow(lengtha, 2) + Math.pow(lengthc, 2) - 2 * lengtha * lengthc * this.$util.cos(angleb))
  167. anglea = this.$util.asin((lengtha * this.$util.sin(angleb)) / lengthb)
  168. anglec = 180 - anglea - angleb
  169. }else if(anglec){
  170. anglea = this.$util.asin((lengtha * this.$util.sin(anglec)) / lengthc)
  171. lengthb = ((2 * lengthc * this.$util.cos(anglea)) + Math.sqrt(Math.pow(2 * lengthc * this.$util.cos(anglea), 2) - 4 * (Math.pow(lengthc, 2) - Math.pow(lengtha, 2)))) / 2
  172. if(!this.$util.checkTriangle(lengtha, lengthb, lengthc)){
  173. lengthb = ((2 * lengthc * this.$util.cos(anglea)) - Math.sqrt(Math.pow(2 * lengthc * this.$util.cos(anglea), 2) - 4 * (Math.pow(lengthc, 2) - Math.pow(lengtha, 2)))) / 2
  174. }
  175. angleb = 180 - anglec - anglea
  176. }
  177. }else if(lengthb && lengthc){
  178. if(anglea){
  179. lengtha = Math.sqrt(Math.pow(lengthb, 2) + Math.pow(lengthc, 2) - 2 * lengthb * lengthc * this.$util.cos(anglea))
  180. angleb = this.$util.asin((lengthb * this.$util.sin(anglea)) / lengtha)
  181. anglec = 180 - anglea - angleb
  182. }else if(angleb){
  183. // sinA=(a×sinC)÷c
  184. anglec = this.$util.asin((lengthc * this.$util.sin(angleb)) / lengthb)
  185. lengtha = ((2 * lengthc * this.$util.cos(angleb)) + Math.sqrt(Math.pow(2 * lengthc * this.$util.cos(angleb), 2) - 4 * (Math.pow(lengthc, 2) - Math.pow(lengthb, 2)))) / 2
  186. if(!this.$util.checkTriangle(lengtha, lengthb, lengthc)){
  187. lengtha = ((2 * lengthc * this.$util.cos(angleb)) - Math.sqrt(Math.pow(2 * lengthc * this.$util.cos(angleb), 2) - 4 * (Math.pow(lengthc, 2) - Math.pow(lengthb, 2)))) / 2
  188. }
  189. anglea = 180 - angleb - anglec
  190. }else if(anglec){
  191. lengtha = ((2 * lengthb * this.$util.cos(anglec)) + Math.sqrt(Math.pow(2 * lengthb * this.$util.cos(anglec), 2) - 4 * (Math.pow(lengthb, 2) - Math.pow(lengthc, 2)))) / 2
  192. if(!this.$util.checkTriangle(lengtha, lengthb, lengthc)){
  193. lengtha = ((2 * lengthb * this.$util.cos(anglec)) - Math.sqrt(Math.pow(2 * lengthb * this.$util.cos(anglec), 2) - 4 * (Math.pow(lengthb, 2) - Math.pow(lengthc, 2)))) / 2
  194. }
  195. angleb = this.$util.asin((lengthb * this.$util.sin(anglec)) / lengthc)
  196. anglea = 180 - anglec - angleb
  197. }
  198. } else if(anglea && angleb){
  199. anglec = 180 - anglea - angleb
  200. if(lengtha){
  201. lengthb = (lengtha * this.$util.sin(angleb)) / this.$util.sin(anglea)
  202. lengthc = (lengtha * this.$util.sin(anglec)) / this.$util.sin(anglea)
  203. }else if(lengthb){
  204. lengtha = (lengthb * this.$util.sin(anglea)) / this.$util.sin(angleb)
  205. lengthc = (lengthb * this.$util.sin(anglec)) / this.$util.sin(angleb)
  206. }else if(lengthc){
  207. lengtha = (lengthc * this.$util.sin(anglea)) / this.$util.sin(anglec)
  208. lengthb = (lengthc * this.$util.sin(angleb)) / this.$util.sin(anglec)
  209. }
  210. }else if(anglea && anglec){
  211. angleb = 180 - anglea - anglec
  212. if(lengtha){
  213. lengthb = (lengtha * this.$util.sin(angleb)) / this.$util.sin(anglea)
  214. lengthc = (lengtha * this.$util.sin(anglec)) / this.$util.sin(anglea)
  215. }else if(lengthb){
  216. lengtha = (lengthb * this.$util.sin(anglea)) / this.$util.sin(angleb)
  217. lengthc = (lengthb * this.$util.sin(anglec)) / this.$util.sin(angleb)
  218. }else if(lengthc){
  219. lengtha = (lengthc * this.$util.sin(anglea)) / this.$util.sin(anglec)
  220. lengthb = (lengthc * this.$util.sin(angleb)) / this.$util.sin(anglec)
  221. }
  222. }else if(angleb && anglec){
  223. anglea = 180 - angleb - anglec
  224. if(lengtha){
  225. lengthb = (lengtha * this.$util.sin(angleb)) / this.$util.sin(anglea)
  226. lengthc = (lengtha * this.$util.sin(anglec)) / this.$util.sin(anglea)
  227. }else if(lengthb){
  228. lengtha = (lengthb * this.$util.sin(anglea)) / this.$util.sin(angleb)
  229. lengthc = (lengthb * this.$util.sin(anglec)) / this.$util.sin(angleb)
  230. }else if(lengthc){
  231. lengtha = (lengthc * this.$util.sin(anglea)) / this.$util.sin(anglec)
  232. lengthb = (lengthc * this.$util.sin(angleb)) / this.$util.sin(anglec)
  233. }
  234. }
  235. this.rules.lengtha.value = lengtha;
  236. this.rules.lengthb.value = lengthb;
  237. this.rules.lengthc.value = lengthc;
  238. this.rules.anglea.value = anglea;
  239. this.rules.angleb.value = angleb;
  240. this.rules.anglec.value = anglec;
  241. this.roundRules()
  242. },
  243. roundRules(){
  244. for (const itemKey in this.rules) {
  245. this.rules[itemKey].value = this.$util.round(this.rules[itemKey].value,2);
  246. }
  247. },
  248. initRules(){
  249. for (const itemKey in this.rules) {
  250. this.rules[itemKey].value = "";
  251. }
  252. },
  253. getInputItem(item){
  254. let validate = [];
  255. let placeholder = `请输入${this.rules[item].name}`;
  256. let disabled = false;
  257. for (const itemKey in this.rules) {
  258. if(item !== itemKey && typeof this.formData[itemKey] !== "undefined")
  259. validate.push(this.formData[itemKey]);
  260. }
  261. if(item.indexOf('angle') !== -1 && !this.$util.checkArrayNotNullNumber(validate,3)){
  262. validate = [];
  263. for (const itemKey in this.rules) {
  264. if(itemKey.indexOf('angle') !== -1 && itemKey !== item && typeof this.formData[itemKey] !== "undefined"){
  265. validate.push(this.formData[itemKey]);
  266. }
  267. }
  268. if(this.$util.checkArrayNotNullNumber(validate)){
  269. disabled = true;
  270. placeholder = '无需输入';
  271. }
  272. }else{
  273. if(this.$util.checkArrayNotNullNumber(validate,3)){
  274. disabled = true;
  275. placeholder = '无需输入';
  276. }
  277. }
  278. return {placeholder: placeholder,disabled:disabled};
  279. },
  280. handleClear() {
  281. this.initRules();
  282. this.formData = {
  283. lengtha: '',
  284. lengthb: '',
  285. lengthc: '',
  286. anglea: '',
  287. angleb: '',
  288. anglec: '',
  289. };
  290. }
  291. },
  292. computed:{
  293. lengthAInput(){
  294. return this.getInputItem('lengtha')
  295. },
  296. lengthBInput(){
  297. return this.getInputItem('lengthb')
  298. },
  299. lengthCInput(){
  300. return this.getInputItem('lengthc')
  301. },
  302. angleAInput(){
  303. return this.getInputItem('anglea')
  304. },
  305. angleBInput(){
  306. return this.getInputItem('angleb')
  307. },
  308. angleCInput(){
  309. return this.getInputItem('anglec')
  310. },
  311. showResult(){
  312. let validate = [];
  313. for (const itemKey in this.rules) {
  314. validate.push(this.rules[itemKey].value);
  315. }
  316. return this.$util.checkArrayNotNullNumber(validate,3)
  317. }
  318. }
  319. }
  320. </script>
  321. <style lang="scss" scoped>
  322. @import "@/static/css/math.scss";
  323. </style>