Skip to content

PHP code to draw cos function on image

Published
Reading time
1 min read
Views
1.2k views
PHP code to draw cos function on image

Today i will tell you how to draw cos function in PHP. This is very simple task to draw a cosine function with PHP code. These are functions of Mathematics and used in Numerical Computations. Now below watch the code in PHP language.

<?php
$x = 600;
$y = 315;
$gd = imagecreatetruecolor($x, $y);   //create background image
$red = imagecolorallocate($gd, 255, 255, 255);   //create image
for ($i = 0; $i < 600; $i++) {
  imagesetpixel($gd, 300, $i, $red);  //draw vertical line
  imagesetpixel($gd, $i, 157, $red);  //draw horizontal line
}
$x = 0;
for($i = 90; $i <= 360; $i+=3)
{
 $y =  157 + (50*cos($i));   //get y-axis with farmula
 imagesetpixel($gd, $x+300, $y, $red);  //draw elips
 $x++;
}
header('Content-Type: image/png');
imagepng($gd);
?>

Questions & comments

Did this work for you, or did you hit a different error? Ask below and we will help.

Comments are reviewed before they appear.

More like this

More in Programming

Programming

PHP code to draw ellipse with Standard equation on an image

In this tutorial you will learn how to draw an Ellipse on an image with PHP code. This is mostly used in Numerical Computing and Mathematics to draw graphs from algebraic…

Programming

PHP code to draw a parabola and create image

Hello Friends today i will discuss how to draw a parabola with PHP code and create an image. We will draw this parabola with standard equation. <?php $x = 400; $y = 400;…

Programming

Easy Way To Draw Line On Image in JAVA

In this article you will learn how to draw line on an image in JAVA language. This is very easy way. You also can store this image in your System. BufferedImage img = new…