Skip to content

Commit

Permalink
Added N sided polygon calculation in 2D Shapes Calculator (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuvraj960 authored Feb 24, 2024
1 parent 46a9180 commit 38bce9d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Calculators/2D-Shapes-Calculator/2D-Shapes-Calculator.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@
<option value="rhombus">Rhombus</option>
<option value="trapezoid">Trapezoid</option>
<option value="hexagon">Hexagon</option>
<option value="nsides">N-Sided Polygon</option>
</select>
</div>
<br>
<div class="row" id="nsides-row" style="display: none;">
<label for="nofsides">No. of sides of polygon:</label>
<input type="number" id="nofsides" name="nofsides">
</div><br>
<div class="row" id="length-row" style="display: none;">
<label for="length">Enter Length:</label>
<input type="number" id="length" name="length">
Expand Down
12 changes: 11 additions & 1 deletion Calculators/2D-Shapes-Calculator/2D-Shapes-Calculator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function calculate(event) {
event.preventDefault();
var shape = document.getElementById("shape").value;
var numSides = parseFloat(document.getElementById("nofsides").value);
var length = parseFloat(document.getElementById("length").value);
var width = parseFloat(document.getElementById("width").value);
var radius = parseFloat(document.getElementById("radius").value);
Expand All @@ -11,7 +12,7 @@ function calculate(event) {
if (shape === "") {
resultContainer.textContent = "Please select a shape.";
return;
} else if(shape === "square" || shape === "hexagon"){
} else if(shape === "square" || shape === "hexagon" || shape === "nsides"){
if(isNaN(length)){
resultContainer.textContent = "Enter valid numerical value";
return;
Expand Down Expand Up @@ -75,6 +76,10 @@ function calculate(event) {
perimeter = 6 * length;
area = (3 * Math.sqrt(3) * Math.pow(length, 2)) / 2;
break;
case "nsides":
perimeter = numSides * length;
area = (numSides * length * length) / (4 * Math.tan(Math.PI / numSides));
break;
// Add cases for more shapes as needed
default:
break;
Expand All @@ -88,6 +93,7 @@ function calculate(event) {
var shape = document.getElementById("shape").value;

// Hide all dimension rows initially
document.getElementById("nsides-row").style.display = "none";
document.getElementById("length-row").style.display = "none";
document.getElementById("width-row").style.display = "none";
document.getElementById("radius-row").style.display = "none";
Expand Down Expand Up @@ -125,6 +131,10 @@ function calculate(event) {
case "hexagon":
document.getElementById("length-row").style.display = "block";
break;
case "nsides":
document.getElementById("nsides-row").style.display = "block";
document.getElementById("length-row").style.display = "block";
break;
// Add cases for more shapes as needed
default:
break;
Expand Down
2 changes: 1 addition & 1 deletion Calculators/2D-Shapes-Calculator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ Calculates the area and perimeter of 2D shapes.

## Screenshots :-

![image](https://github.com/Rakesh9100/CalcDiverse/assets/73993775/f7ce979b-e468-4a4e-9008-2e3261b50a2a)
![Screen-Shot](image.png)
Binary file added Calculators/2D-Shapes-Calculator/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 38bce9d

Please sign in to comment.