Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Creating 3D Curves and Surfaces in R

Tech May 7 6

Space Curvess

Praametric Curve Example 1

library(plot3D)

t_vals <- seq(0, 2, length.out = 100)

x_coords <- 2 * t_vals
y_coords <- 3 * t_vals^2
z_coords <- 4 * t_vals^3

scatter3D(x_coords, y_coords, z_coords, col = "blue")

Parametric Curve Example 2

library(plot3D)

t_range <- seq(0, 2*pi, length.out = 100)

x_points <- cos(t_range) + sin(t_range)^2
y_points <- sin(t_range) * (1 - cos(t_range))
z_points <- cos(t_range)

scatter3D(x_points, y_points, z_points, col = "green")

3D Surfaces

Planar Surface

library(plot3Drgl)

par(mai = c(1, 1, 0, 0))

x_axis <- seq(0, 4, length = 100)
y_axis <- seq(0, 2, length = 100)

plane_func <- function(x, y) (4 - x - 2*y) / 3
z_values <- outer(x_axis, y_axis, plane_func)

persp3Drgl(z = z_values, ticktype = "detailed")
planes3d(0, 0, 1, 0, col = "lightgray")

Saddle Surface

library(plot3Drgl)

par(mai = c(0.8, 0.8, 0, 0))

x_range <- seq(-4, 4, length = 100)
y_range <- seq(-3, 3, length = 100)

saddle <- function(x, y) x^2/16 - y^2/9
z_surface <- outer(x_range, y_range, saddle)

persp3Drgl(z = z_surface, ticktype = "detailed")

Spherical Surface

library(plot3Drgl)

mesh_grid = mesh(
  phi = seq(0, 2*pi, length.out = 100),
  theta = seq(0, pi, length.out = 100)
)

phi_vals = mesh_grid$phi
theta_vals = mesh_grid$theta
radius = 2

x_sphere = radius * sin(theta_vals) * cos(phi_vals)
y_sphere = radius * sin(theta_vals) * sin(phi_vals)
z_sphere = radius * cos(theta_vals)

surf3D(x_sphere, y_sphere, z_sphere, colvar = z_sphere)

Intersecting Surfaces

Example: Intersection of two surfaces

library(plot3Drgl)

x_domain <- seq(-2, 2, length.out = 150)
y_domain <- seq(-2, 2, length.out = 150)

elliptic_paraboloid <- function(x, y) x^2 + 2*y^2
z_elliptic <- outer(x_domain, y_domain, elliptic_paraboloid)

parabolic_cylinder <- function(x, y) 2 - x^2
z_parabolic <- outer(x_domain, y_domain, parabolic_cylinder)

persp3Drgl(z = z_elliptic, col = "orange")
persp3Drgl(z = z_parabolic, add = TRUE, col = "blue")
planes3d(0, 0, 1, 0, col = "lightgray")

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.