Python Forum
How can I design shapes on a curve in python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I design shapes on a curve in python?
#1
I have coordinates of an airfoil curve. But I want to design semi-circular shapes that have equal distance of center on this curve's upper line.
I prepared the code like this, but it didn't give me the new design. What else can I do?

import matplotlib.pyplot as plt
import numpy as np

# Airfoil coordinates (replace with your actual airfoil data)
# Format: [x, y]
airfoil_coordinates = np.array([
    [1.0, 0.0],
[0.997397 ,	0.00066484],
[0.98930099 ,0.00263811],
[0.9758986 , 0.00585581],
[0.95732594, 0.01021599],
[0.93377286, 0.01558451],
[0.90548195, 0.02180217],
[0.87274705,	0.02869202],
[0.83591106,	0.03606593],
[0.79536313,	0.04372985],
[0.7515351,	0.05148739],
[0.70489738,	0.0591418],
[0.65595428,	0.06649689],
[0.60523895,	0.07335757],
[0.55330789,	0.07953095],
[0.50073524,	0.08482876],
[0.44810653,	0.08907167],
[0.39597224,	0.09209455],
[0.34447681,	0.09354874],
[0.29469369,	0.09319491],
[0.247217,	0.09101236],
[0.20261449,	0.08704035],
[0.16141717,	0.08137882],
[0.1241097,	0.07418436],
[0.09112218,	0.06566122],
[0.06282393,	0.05604757],
[0.03951943,	0.04559798],
[0.0214464,	0.03456355],
[0.00877581,	0.02317172],
[0.00161308,	0.01160775],
[0	       ,  0],
[0.00386502,	-0.01105828],
[0.01307659,	-0.02100956],
[0.02749709,	-0.02980941],
[0.04693512,	-0.03740803],
[0.07115066,	-0.04375885],
[0.09986083,	-0.04882884],
[0.13274548,	-0.05260868],
[0.16945222,	-0.05512141],
[0.20960026,	-0.05642796],
[0.252783,	-0.05662861],
[0.29856967,	-0.05586008],
[0.3465062,	-0.05428834],
[0.39611607,	-0.05209824],
[0.44736501,	-0.04932603],
[0.49926476,	-0.04594219],
[0.55122057,	-0.04211028],
[0.60267274,	-0.03798361],
[0.65306271,	-0.03369861],
[0.70183926,	-0.02937242],
[0.7484649,	-0.02510332],
[0.79242212,	-0.02097352],
[0.83321955,	-0.01705316],
[0.87039778,	-0.01340466],
[0.90353504,	-0.01008621],
[0.93225254,	-0.00715385],
[0.95621952,	-0.00466171],
[0.97515792,	-0.00266062],
[0.98884661,	-0.00119526],
[0.9971249,	-0.00030089],
[1.0, 0.0]
])

# Dimple parameters
dimple_radius = 0.05  # Radius of the semi-circle dimple
dimple_spacing = dimple_radius * 2  # Distance between centers of the dimples

# Calculate the number of dimples based on the airfoil length and spacing
num_dimples = int((airfoil_coordinates[-1, 0] - airfoil_coordinates[0, 0]) / dimple_spacing) #+1 vardı

# Create semi-circle dimples
def create_dimple(x, y, radius):
    distance_to_center = np.sqrt(x ** 2 + y ** 2)
    if distance_to_center <= radius:
        y_offset = np.sqrt(radius ** 2 - x ** 2)
        return y_offset
    return y

dimpled_airfoil_coordinates = airfoil_coordinates.copy()
for i in range(num_dimples):
    dimple_x = i * dimple_spacing
    dimpled_airfoil_coordinates[:, 1] = np.vectorize(create_dimple)(
        airfoil_coordinates[:, 0] - dimple_x,
        dimpled_airfoil_coordinates[:, 1],
        dimple_radius
    )

# Plot the airfoil with dimples
plt.plot(airfoil_coordinates[:, 0], airfoil_coordinates[:, 1], label="Airfoil")
plt.plot(dimpled_airfoil_coordinates[:, 0], dimpled_airfoil_coordinates[:, 1], label="Airfoil with Dimples")

plt.xlabel('X-coordinate')
plt.ylabel('Y-coordinate')
plt.title('Airfoil with Semi-Circle Dimples')
plt.legend()
plt.axis('equal')
plt.grid(True)
plt.show()
Larz60+ write Aug-06-2023, 05:44 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#2
Well, I think there are some logical error with your code.
Try below code:

import matplotlib.pyplot as plt
import numpy as np

# Airfoil coordinates (replace with your actual airfoil data)
# Format: [x, y]
airfoil_coordinates = np.array([
    # ... (your airfoil coordinates)
])

# Dimple parameters
dimple_radius = 0.05  # Radius of the semi-circle dimple
dimple_spacing = dimple_radius * 2  # Distance between centers of the dimples

# Create semi-circle dimples
def create_dimple(x, y, radius):
    distance_to_center = np.sqrt(x ** 2 + y ** 2)
    if distance_to_center <= radius:
        y_offset = np.sqrt(radius ** 2 - x ** 2)
        return y_offset
    return y

dimpled_airfoil_coordinates = airfoil_coordinates.copy()
for i in range(len(airfoil_coordinates)):
    dimple_x = airfoil_coordinates[i, 0]  # Use the x-coordinate of the airfoil
    dimpled_airfoil_coordinates[i, 1] = np.vectorize(create_dimple)(
        airfoil_coordinates[:, 0] - dimple_x,
        dimpled_airfoil_coordinates[:, 1],
        dimple_radius
    )

# Plot the airfoil with dimples
plt.plot(airfoil_coordinates[:, 0], airfoil_coordinates[:, 1], label="Airfoil")
plt.plot(dimpled_airfoil_coordinates[:, 0], dimpled_airfoil_coordinates[:, 1], label="Airfoil with Dimples")

plt.xlabel('X-coordinate')
plt.ylabel('Y-coordinate')
plt.title('Airfoil with Semi-Circle Dimples')
plt.legend()
plt.axis('equal')
plt.grid(True)
plt.show()
I hope it will work.
Thanks
deanhystad write Sep-13-2023, 05:08 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#3
I wish I knew what this all means!

Anyway, if I use:

for i in range(num_dimples):
    dimple_x = i * dimple_spacing
    dimpled_airfoil_coordinates[:, 1] = np.vectorize(create_dimple)(
        airfoil_coordinates[:, 0] - dimple_x,
        dimpled_airfoil_coordinates[:, 1],
        dimple_radius
    )
I get this error:

Output:
TypeError: only size-1 arrays can be converted to Python scalars The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode exec(code, self.locals) File "<pyshell#9>", line 3, in <module> ValueError: setting an array element with a sequence.
I don't know what : in dimpled_airfoil_coordinates[:, 1] is supposed to do, but it causes an error.

But if I use this, changing : for i, then I get the plot of a crosssection through an aeroplane wing, lots of short lines on top:

for i in range(len(airfoil_coordinates)):
    # i is range(0, 61) meaning 0 to 60
    dimple_x = airfoil_coordinates[i][0]  # Use the x-coordinate of the airfoil
    dimpled_airfoil_coordinates[i, 1] = np.vectorize(create_dimple)(
        airfoil_coordinates[i, 0] - dimple_x,
        dimpled_airfoil_coordinates[i, 1],
        dimple_radius
    )
dimple_x is simply the x coordinate of airfoil_coordinates[i]
dimpled_airfoil_coordinates[i, 1] is the y coordinate of dimpled_airfoil_coordinates[i] as I understand it!

Maybe that helps!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Learning curve astral_travel 3 443 Apr-05-2024, 06:58 PM
Last Post: jefsummers
  Fitting data to a curve daaegp 1 625 Jun-30-2023, 08:06 PM
Last Post: Gribouillis
  x and y must have same first dimension, but have shapes (1,) and (50,) asja2010 5 2,634 Jan-12-2023, 07:24 PM
Last Post: deanhystad
  Shapes in Video finndude 0 669 Oct-07-2022, 03:30 PM
Last Post: finndude
  Shapes over video in tkinter finndude 1 973 Oct-04-2022, 06:14 PM
Last Post: deanhystad
  operands could not be broadcast together with shapes (337,451) (225,301) kevinabbot 0 1,572 Dec-14-2021, 04:02 PM
Last Post: kevinabbot
  Find factor to match test curve to golden curve SriRajesh 0 1,566 Jun-17-2021, 04:39 AM
Last Post: SriRajesh
  Fitting Gaussian curve to data file Laplace12 0 2,767 Jun-09-2021, 10:45 AM
Last Post: Laplace12
  ValueError: x and y must have same first dimension, but have shapes (11,) and (15406, hobbyist 17 150,751 Mar-22-2021, 10:27 AM
Last Post: hobbyist
  search of a curve fitting function bluffy5 2 2,438 Dec-13-2020, 09:53 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020